How to pass an NSError object from a background thread to the main thread without violating memory management rules?
I am doing numerous operations such as retrieval from web, importing, file operations, etc. in a background thread. Each operation is called using NSInvocationOperation
. Once each operation is complete, the background thread operation calls performSelectorOnMainThread:
. The model I am using is for the background thread to call operationSucceeded
or operationFailed
depending on the success or failure of the background operation.
When I call operationFailed
, I want to pass an NSError
object back to the main thread so that the thread can take any appropriate action with the UI. In the case of the failure, for example, I may or may not want to display an error message to the user.
The question is: How can I send the NSError
back to t开发者_如何学编程he main thread without running into memory management concerns? Each operation that is called using NSInvocationOperation
has it's own local NSAutoreleasePool
per Apple requirements. When I pass back an NSError
object using performSelectorOnMainThread:withObject:error
, doesn't the local NSAutoreleasePool
release the NSError
object?
You don't need to do anything. As is explained in the reference, performSelectorOnMainThread:@selector(foo:) withObject:obj
retains obj
until the selector is executed.
精彩评论