Nick Harris

Retain Cycle with NSOperation Dependencies – FIXED

leave a comment »

I knew it was going to be something simple. 

Even though this doesn’t work:

let transferOperation = NSBlockOperation() {
    [weak operationOne, operationTwo] in
            
    print("transferOperation")
    if let createdObject = operationOne?.createdObject {
        operationTwo.passedInObject = createdObject
    }
}

 

this does:

let transferOperation = NSBlockOperation() {

    [weak operationTwo, weak operationOne] in

            

    print(“transferOperation”)

    if let createdStrings = operationOne?.createdStrings {

        operationTwo?.passedInStrings = createdStrings

    }

}

 

Why? I’m not sure yet. But glad I asked and got some more eyes on the problem.

Thanks Josh!

UPDATE

I should have read the docs closer. The first doesn’t work because operationTwo is still a strong reference. I mistakenly thought that it would still take on the weak declared with operationOne but after re-reading the documentation:

Defining a Capture List

Each item in a capture list is a pairing of the weak or unowned keyword with a reference to a class instance (such as self) or a variable initialized with some value (such as delegate = self.delegate!). These pairings are written within a pair of square braces, separated by commas.

I wish the compiler would have warned me. At least I know better now.

Advertisements

Written by Nick Harris

February 3, 2016 at 10:55 pm

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: