I’m using RestKit to develop a RESTful application. I have a wrapper object that actually handles requests and even acts as the delegate for the RKObjectManager. I am experiencing an issue related to how ARC handles instance variables and retaining them, and it is clear to me that I do not understand how ARC works.
So when I do this, it fails (with error related to message sent to deallocated instance)
MyTestClient *testClient = [[MyTestClient alloc] init];
but when I declare a property and do this, all is fine:
self.testClient = [[MyTestClient alloc] init];
From what I understand, under ARC, an instance variable is always strong by default, but it’s lifecycle is the scope of the method in which it’s declared.
Since I can’t do [testClient retain], is my only option to make it a property?
Your variables are strong by default and will be retained within their scope. So in the first example, the object will only be retained until the end of the function. In the second, since you have an instance variable, it will be retained until the owning object is deallocated (presumably long enough for you in this case). Your best option is to make it a property, but you could also just make it an instance variable. It will do the same thing for you in this case.
A simple way to think about it is you need to have a strong pointer to an object until you no longer need it around.