I am trying to split up my programs flow using NSOperations. I am using the Parse framework to make a simple messaging app. I want to display some messages and then delete them. The display messages shouldn't be called unless the delete operations is finished so I want to try using an NSQueue and add a displayMessages operation to it and then a deleteMessages operation (named MyOperation below). I understand that concurrent operations means they will only execute one after another in a queue fashion. Below is my code for the delete method. Is there a way to manually tell the operation it is finished i.e. setting isFinished or isExecuting??
// MyOperation.h
@interface MyOperation : NSOperation {
@property (strong, nonatomic) NSMutableArray *toDelete;
}
@end
And the implementation:
// MyOperation.m
@implementation MyOperation
- (id)initWithArray:(NSMutableArray *)array
{
self = [super init];
if (self == nil)
return nil;
_toDelete=array;
}
- (void)main {
if ([self isCancelled]) {
NSLog(@"** operation cancelled **");
}
//how do I get main to finish execution ONLY after deleteAllInBackground has finished?
[PFObject deleteAllInBackground:self.toDelete];
if ([self isCancelled]) {
NSLog(@"** operation cancelled **");
}
NSLog(@"Operation finished");
}
@end
right now this code above won't solve my problem. It will queue up the ops but this one will finish even though the deleteAllInBackground is still running. Would really appreciate some help here! thanks
other possible solution:
-(void)receivedMessage
{
NSLog(@"push!");
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[self displayMessages];
dispatch_async(dispatch_get_main_queue(), ^(void){
if([self.toDelete count]>0) {
[PFObject deleteAllInBackground:self.toDelete];
}
});
});
}
Use
deleteAll
instead ofdeleteAllInBackground
if you want to block the current thread until the deletion is complete.I would suggest you to use
dispatch_async
like below;If you have to use
NSOperationQueue
then I would suggest you to useKVO
to get notification for task completion; When you setup your queue, do this:Then do this in your
observeValueForKeyPath
:[EDIT]