English 中文(简体)
ARCA的人工物体寿命
原标题:Manual object lifetime with ARC

研究了以下法典,并假设该守则是在阿拉伯研究中心下汇编的:

- (void)foo {
    NSOperationQueue *oq = [[NSOperationQueue alloc] init];
    [oq addOperationWithBlock:^{
        // Pretend that we have a long-running operation here.
    }];
}

尽管作业地点被宣布为当地变量,但其寿命只要运行,就继续超出该方法的范围。

如何做到这一点?

<><>UPDATE:

我赞赏大家深思熟虑的评论,但我认为我没有正确地问我的问题。 我并不是要问一个具体的问题,即乌克兰行动,而是关于阿农发委员会中物体寿命的一般问题。 具体地说,我的问题是:

How,根据ARC,物体能否参与对其一生的管理?

我是一位方案家,时间很长,我非常清楚这种事情的陷阱。 我不想说这是好还是坏的想法。 我认为,这总的来说是坏的。 相反,我的问题是学术问题: 无论它是否具有良好或坏的思想,ARC如何做到这一点,以及具体辛迪加如何这样做?

最佳回答

作为一般情况,你可以经常提到紧急情况。 例如:

@implementation MasterOfMyOwnDestiny
{
   MasterOfMyOwnDestiny *alsoMe;
}

- (void) lifeIsGood
{
    alsoMe = self;
}

- (void) woeIsMe
{
    alsoMe = nil;
}

...

@end
问题回答

这里有几个可能性:

  1. <代码>NSOperation Queue 留守,直至其空出为止。

  2. The NSOperationQueue causes some other object to retain it. For example, since NSOperationQueue uses GCD, perhaps addOperationWithBlock: looks something like this:

    - (void)addOperationWithBlock:(void (^)(void))block {
        void (^wrapperBlock)(void) = ^{
            block();
            [self executeNextBlock];
        };
        if (self.isCurrentlyExecuting) {
            [self.queuedBlocks addObject:wrapperBlock];
        } else {
            self.isCurrentlyExecuting = YES;
            dispatch_async(self.dispatchQueue, wrapperBlock);
        }
    }
    

    该代码wrapperBlock 其中载有对<代码>NSOperation Queue的有力参考,因此(假定ARC),它保留了NSOperation。 Queue。 (真实的<代码>addOperationWithBlock:比此更为复杂,因为它既有安全又有支持同时执行多个区块。)

  3. The NSOperationQueue doesn t live past the scope of your foo method. Maybe by the time addOperationWithBlock: returns, your long-running block has already been submitted to a GCD queue. Since you don t keep a strong reference to oq, there is no reason why oq shouldn t be deallocated.

举例来说,根据ARC,在ARC公司的领导下,被抓住的有地方的“国家安全局”行动。 基本上,这块块块地可以节省点人的价值,以便日后从地块内获取。 不管你是否重新使用杀伤人员地雷,实际上都会发生这种情况;差别在于,根据《大气污染法》,物体变量自动保留和释放,因为碎片被复制和释放。

The section "Object and Block Variables" in the Blocks Programming Topics guide is a good reference for this stuff.

我认为,最简单的一件事是拥有一个全球NSMutableArray(或确定,或不管怎样),该物体会给自己带来好处,并从中去除。 另一种想法是,将(如你已经承认的)单程式管理代码归入非美国航天中心档案中的一个类别,并直接使用——即保留和——释放。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...