同步处理推送通知操作中的操作

时间:2016-11-07 12:37:31

标签: ios objective-c push-notification push

我希望使用push notification同步处理FIFO个动作。内:

(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo   completionHandler:(void (^)())completionHandler

我有很多块,我希望当我收到一个动作时,我将它(带有它的块)添加到FIFO

2 个答案:

答案 0 :(得分:0)

NSOperationQueue* queue = [[ NSOperationQueue alloc ] init];
queue.maxConcurrentOperationCount = 1;

NSOperation* someOperation = [NSBlockOperation blockOperationWithBlock:^(void) { NSLog(@"Done.");}];

if (queue.operations.count != 0)
    [someOperation addDependency: queue.operations.lastObject];

您也可以简单地在NSOperationQueue中添加一个类别:

@interface NSOperationQueue (FIFOQueue)
  - (void)addOperationAfterLast:(NSOperation *)op;
@end

@implementation NSOperationQueue (FIFOQueue)
- (void)addOperationAfterLast:(NSOperation *)op
  {
    if (self.maxConcurrentOperationCount != 1)
        self.maxConcurrentOperationCount = 1;
        NSOperation* lastOp = self.operations.lastObject;
    if (lastOp != nil)
        [op addDependency: lastOp];
        [self addOperation:op];
    }
@end

并使用[queue addOperationAfterLast:myOperation]。

答案 1 :(得分:0)

尝试使用此方法

dispatch_queue_t myQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
dispatch_async(myQueue, ^{
  printf("1");
});
dispatch_async(myQueue, ^{
  printf("2");
});
dispatch_async(myQueue, ^{
  printf("3");
});
dispatch_async(myQueue, ^{
  printf("4");
});
dispatch_async(myQueue, ^{
  printf("5");
});
// wait for queue to empty
dispatch_sync(myQueue, ^{
  printf("done");
});