Skip to content

Instantly share code, notes, and snippets.

@TonnyXu
Created August 2, 2012 04:54
Show Gist options
  • Save TonnyXu/3233756 to your computer and use it in GitHub Desktop.
Save TonnyXu/3233756 to your computer and use it in GitHub Desktop.
`-init` method is called from the caller's thread | CoreData Pitfall

Refresh: How to use NSOperation

Using NSOperation is easy, typically, it is used as below:

Create your own NSOperation subclass

@interface MyOperation : NSOperation
@end

@implementation MyOperation
- (id)init{}
- (void)start{// do you job here}
- (void)main{// or do your job here}
@end

Add NSOperation instance to NSOperationQueue

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
MyOperation *myOperation = [[MyOperation alloc] init];  // Here is the point
[myQueue addOperation:myOperation];

Biggest Problem

Don't forget that your NSOperation object is created and initialized in the caller's thread. If you are using CoreData and put the instantiation of NSManagedObjectContext in the -init method, you're wrong. Because the -init method is called in the caller's thread which is not what you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment