我正在为网络请求创建一个类,比如NetworkManager,它是一个单例。我希望这个类处理网络请求。
我有两个NSOperationQueues,一个用于并行请求,其中没有设置numberofConcurrent Operations。对于顺序队列,要将setMaxConcurrentOperationCount设置为1。我想,第二个NSOperation请求将在第一个请求执行结束后执行。它只运行第一个NSOperation,它处理网络请求。如果我必须按顺序处理请求,该怎么办?下面是示例代码:
NSOperation *networkOperation = [[NetworkOperation alloc]initWithRequest:request networkRequest:networkRequest];
if(!sequentialQueue){
sequentialQueue = [[NSOperationQueue alloc]init];
[sequentialQueue setMaxConcurrentOperationCount:1];
}
[sequentialQueue addOperation:networkOperation];请帮帮忙。
发布于 2014-05-31 09:19:42
您可以对NSOperationQueue进行子类化,并像这样重写- addOperation:方法。
- (void)addOperation:(NSOperation *)operation {
// check whether the queue has at least one operation, you can also check for nil
if ([[self operations] count] > 0) [operation addDependency:[self.operations lastObject]];
[super addOperation:operation];
}发布于 2014-05-31 08:40:23
您的问题听起来需要调整操作的顺序,
- (void)addDependency:(NSOperation *)operation方法的NSOperation说,你必须在一个特定的操作后进行操作。让我们举一个例子:
NSOperation *firstOperation = [NSOperation new]; // do your own init
NSOperation *secoundOperation = [NSOperation new];
[secoundOperation addDependency:firstOperation];在这里,secoundOperation将在firstOperation之后得到保证。如果只设置- maxConcurrentOperationCount,则不能保证操作的顺序。
希望它能帮到你。
https://stackoverflow.com/questions/23968040
复制相似问题