Reputation: 343
I started a thread in a UIview as a background thread which transfer data for the view. However, crash happens in such situation: When I left the view at the very time that the thread is trying to transfer data.
I didn't get quite clear with the relationship between the UIview object and thread. I guess it crashes because the thread was trying to visit UIview members or methods, which were not existed any more. So, I wonder what happened to the thread if the UIView which detach it has been left.
This is my detaching code:
- (void)reloadData {
isLoaded = NO; //UIView member.
[NSThread detachNewThreadSelector:@selector(getThreadInAnotherThread) toTarget:self withObject:nil];
}
- (void)getThreadInAnotherThread {
//Loading code
isLoaded = YES;
[self performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:YES];
}
And I didn't do anything in viewDidDisappear.
Upvotes: 0
Views: 486
Reputation: 56
As the documentation for detachNewThreadSelector:toTarget:withObject states:
The objects aTarget and anArgument are retained during the execution of the detached thread, then released.
And the same for performSelectorOnMainThread:withObject:waitUntilDone:
This method retains the receiver and the arg parameter until after the selector is performed.
If its still not clear what is happening here consult the cocoa memory management guide.
Upvotes: 1
Reputation: 5861
There is no relationship between the view and the thread unless you put one there yourself.
Upvotes: 0