Using NSOperation is easy, typically, it is used as below:
@interface MyOperation : NSOperation
@end
@implementation MyOperation
- (id)init{}
- (void)start{// do you job here}
- (void)main{// or do your job here}
@end
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
MyOperation *myOperation = [[MyOperation alloc] init]; // Here is the point
[myQueue addOperation:myOperation];
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.