If you have used mrr,autorelease this keyword should be too familiar, each time we generate a new object return, we need to send an autorelease message to this object, in order to delay the release of the created object. When is it that the object will be released? Is there any way to release a Autorelease object faster?
Let's start by looking at a phenomenon:
@property (Weak, nonatomic) NSString *weakstring;- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.NSString*hello = [[NSString alloc] initwithcstring:"It is released at the end of current Runloop"encoding:nsutf8stringencoding]; Self.weakstring=Hello; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd));}- (void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated]; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd));}- (void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd)); Dispatch_async (Dispatch_get_main_queue (),^{NSLog (@"-2--%@-%@", Self.weakstring, Nsstringfromselector (_cmd)); });}
We created a Viewdidload method that creates a weak pointer to a string, and when the code executes to Viewwillappear: and viewdidappear: We can still print the string that the weak pointer points to, When you perform a print operation in Viewdidappear: In the next runloop, the weak pointer points to nil, why?
We can completely translate this statement to MRR:self.weakString = Hello
self.weakstring = [[Hello retain] autorelease];
So, can we guess that Autorelease's object was released after the current Runloop ended?
There are a few things that we need to clarify:
1) autorelease Pool
The release time of Autorelease in Objective-c