Ios NSOperationQueue不限制并发操作
并发操作的数量限制为一个,但除非它们都启动。假设添加了三个操作,控制台中的输出如下:Ios NSOperationQueue不限制并发操作,ios,objective-c,nsoperation,nsoperationqueue,Ios,Objective C,Nsoperation,Nsoperationqueue,并发操作的数量限制为一个,但除非它们都启动。假设添加了三个操作,控制台中的输出如下: NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; myQueue.MaxConcurrentOperationCount = 1; [myQueue addOperationWithBlock: ^ { NSLog(@"started"); [self aVeryLongOperation]
NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
myQueue.MaxConcurrentOperationCount = 1;
[myQueue addOperationWithBlock: ^ {
NSLog(@"started");
[self aVeryLongOperation];
}];
如何将并发操作的数量限制为一个?问题是在函数
aVeryLongOperation
中产生了一个后台线程,所有工作都是在该线程上完成的。因此,该函数在生成该线程后立即返回,下一个任务将从队列中执行 在调用aVeryLongOperation
-NSLog(@“end”)后添加一个log语句代码>。输出是什么?以编写的代码为例,您似乎正在创建三个NSOperationQueue。并发操作限制为每个队列。是的,我确实创建了三个NSOperationQueues。所以我改变了这一点,将“myQueue”作为一个属性,但不幸的是控制台日志是相同的。
2014-09-09 17:51:02.705 Sample App [7356:7e03] started
2014-09-09 17:51:03.840 Sample App [7356:7e03] started
2014-09-09 17:51:04.495 Sample App [7356:7e03] started