My problem is the following: inside a method I'm creating an object like this:
MyObject* myObject = [MyObject new];
Then I want it to perform an asynchronous task like this:
[myObject doSomethingAsyncWithCompletion:^{
//do something else upon completion
}];
This brings me to a dilemma of how to retain this object. If I don't then it will get deallocated by the end of my method and then who knows what may happen: the completion block may not be called or it may crash or anything. I would like to be sure that everything will go well.
So I have the following options:
Create a strong property and assign the object to the property:
self.myObject = myObject;
but it seems to be ugly. Creating an additional property ONLY to retain a local object seems to be quite excessive not talking about handling hypothetic multithreading
Make
myObject
explicitly retain itself (by capturing self in the completion block for example) but it also seems to look weird. I'm used to think that this is the responsibility of the calling code to perform all the memory management. The idea of object handling its own memory management is pretty unusual, isn't it?
So what do I do?