我发现maxConcurrentOperationCount=1并不管用 虽然网上搜到一大堆说添加依赖能解决问题 但现实中有如下api
CMStepCounter ios8苹果计步器
(void)queryStepCountStartingFrom:(NSDate *)start
to:(NSDate *)end
toQueue:(NSOperationQueue *)queue
withHandler:(CMStepQueryHandler)handler;
和CMMotionManager中方法
(void)startDeviceMotionUpdatesToQueue:(NSOperationQueue *)queue withHandler:(CMDeviceMotionHandler)handler
都是异步回调的
怎么实现如下功能
for(int i = 0; i< 24 ;i ++)
{
[_stepCounter queryStepCountStartingFrom:fromDate
to:toDate
toQueue:_timeQueue
withHandler:^(NSInteger numberOfSteps, NSError *error)
{
}
}
按顺序取出
滿天的星座2017-04-27 09:05:07
maxConcurrentOperationCount will definitely work. One-step interface serialization, you have to send the time (condition variable, signal, etc.) after receiving the callback
天蓬老师2017-04-27 09:05:07
maxConcurrentOperationCount=1
只能保证一次只有一个operation
在执行,但并不能够保证将所有加入的operation
严格按序执行。
如果需要让加入的所有operation
都严格按序执行的话,最简单的方法就是每次给加入的operation
Add dependency.
Similar to this:
NSOperation *operationToAdd;
NSOperation *lastOperation = queue.operations.lastObject;
if (lastOperation) {
[operationToAdd addDependency:lastOperation];
}
[queue addOperation:operationToAdd];
As far as the question in your question is concerned, although the final callbacks of the two functions you gave are asynchronous, it does not mean that they cannot be implemented using operation
.
You can subclass a NSOperation
yourself, perform an asynchronous operation and end it NSOperation
,执行异步操作然后在回调里面把operation
结束掉。
这样一个异步的调用就转成了一个operation
in the callback.