可可 – NSOperation等待直到异步块执行

可可 – NSOperation等待直到异步块执行,第1张

概述我需要将异步 *** 作放入一个 *** 作队列中,但是它们需要相互依次执行 self.operationQueue = [NSOperationQueue new];self.operationQueue.maxConcurrentOperationCount = 1;[self.operationQueue addOperationWithBlock:^{ // this is asynchr 我需要将异步 *** 作放入一个 *** 作队列中,但是它们需要相互依次执行

self.operationQueue = [NSOperationQueue new];self.operationQueue.maxConcurrentoperationCount = 1;[self.operationQueue addOperationWithBlock:^{    // this is asynchronous    [peripheral1 connectWithCompletion:^(NSError *error) {    }];}];[self.operationQueue addOperationWithBlock:^{    // this is asynchronous    [peripheral2 connectWithCompletion:^(NSError *error) {    }];}];

问题是,由于peripheralN connectWithCompletion是异步的,队列中的 *** 作已经结束,下一个执行,所以我需要模拟,外围连接WithCompletion是同步的,等待 *** 作结束,直到异步块执行

所以我需要这样的行为,只使用 *** 作队列

[peripheral1 connectWithCompletion:^(NSError *error) {            [peripheral2 connectWithCompletion:^(NSError *error) {            }];    }];
解决方法 NSBlockOperation不能处理异步 *** 作,但创建NSOperation的子类并不是那么难,

基本上,您需要创建一个NSOperation,它将一个块作为一个完成处理程序.该块可以这样定义:

typedef voID(^AsyncBlock)(dispatch_block_t completionHandler);

然后,在NSOperation子类的start方法中,您需要调用AsyncBlock传递一个dispatch_block_t,当执行完毕后将被调用.您还需要确保保持KVO符合NSOperation的isFinished和isExecuting属性(至少)(参见NSOperation文档中的KVO-Compliant Properties);这是NSOperationQueue等待直到异步 *** 作完成为止.

这样的事情

- (voID)start {    [self willChangeValueForKey:@"isExecuting"];    _executing = YES;    [self dIDChangeValueForKey:@"isExecuting"];    self.block(^{        [self willChangeValueForKey:@"isExecuting"];        _executing = NO;        [self dIDChangeValueForKey:@"isExecuting"];        [self willChangeValueForKey:@"isFinished"];        _finished = YES;        [self dIDChangeValueForKey:@"isFinished"];    });}

请注意,_executing和_finished需要在您的子类中定义某处,您需要重写isExecuting和isFinished属性才能返回正确的值.

如果将所有这些都放在一起,以及一个启动AsyncBlock的初始化程序,那么可以将这些 *** 作添加到队列中:

[self.operationQueue addOperationWithBlock:^(dispatch_block_t completionHandler) {    [peripheral1 connectWithCompletion:^(NSError *error) {        // call completionHandler when the operation is done        completionHandler();    }];}];[self.operationQueue addOperationWithBlock:^(dispatch_block_t completionHandler) {    [peripheral2 connectWithCompletion:^(NSError *error) {        // call completionHandler when the operation is done        completionHandler();    }];}];

我在这里总结了一个简单版本的要点:AsyncOperationBlock.
这只是一个最低限度的实现,但它应该有效(例如,如果isCancelled也可以实现,那将会很好).

在这里复制完整性:

AsyncBlockOperation.h:

#import <Foundation/Foundation.h>typedef voID(^AsyncBlock)(dispatch_block_t completionHandler);@interface AsyncBlockOperation : NSOperation@property (nonatomic,Readonly,copy) AsyncBlock block;+ (instancetype)asyncBlockOperationWithBlock:(AsyncBlock)block;- (instancetype)initWithAsyncBlock:(AsyncBlock)block;@end@interface NSOperationQueue (AsyncBlockOperation)- (voID)addAsyncoperationWithBlock:(AsyncBlock)block;@end

AsyncBlockOperation.m:

#import "AsyncBlockOperation.h"@interface AsyncBlockOperation () {    BOol _finished;    BOol _executing;}@property (nonatomic,copy) AsyncBlock block;@end@implementation AsyncBlockOperation+ (instancetype)asyncBlockOperationWithBlock:(AsyncBlock)block {    return [[AsyncBlockOperation alloc] initWithAsyncBlock:block];}- (instancetype)initWithAsyncBlock:(AsyncBlock)block {    if (self = [super init]) {        self.block = block;    }    return self;}- (voID)start {    [self willChangeValueForKey:@"isExecuting"];    _executing = YES;    [self dIDChangeValueForKey:@"isExecuting"];    self.block(^{        [self willChangeValueForKey:@"isExecuting"];        _executing = NO;        [self dIDChangeValueForKey:@"isExecuting"];        [self willChangeValueForKey:@"isFinished"];        _finished = YES;        [self dIDChangeValueForKey:@"isFinished"];    });}- (BOol)isFinished {    return _finished;}- (BOol)isExecuting {    return _executing;}- (BOol)isAsynchronous {    return YES;}@end@implementation NSOperationQueue (AsyncBlockOperation)- (voID)addAsyncoperationWithBlock:(AsyncBlock)block {    [self addOperation:[AsyncBlockOperation asyncBlockOperationWithBlock:block]];}@end
总结

以上是内存溢出为你收集整理的可可 – NSOperation等待直到异步块执行全部内容,希望文章能够帮你解决可可 – NSOperation等待直到异步块执行所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1029275.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存