views:

103

answers:

2

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.

A: 

There is no relationship between the view and the thread unless you put one there yourself.

Azeem.Butt
So, if I left the UIview and thread is still trying to visit it's member, it would crash? When did UIview release after I left it?
A: 

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.

zupamario
Thanks! I got it.