ios - Alamofire 如何将request添加到一个队列里面,顺序执行??
黄舟
黄舟 2017-04-18 09:33:38
0
3
844

如题,Alamofire 如何将request添加到一个队列里面,顺序执行??

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(3)
Ty80

Use NSOperationQueue

伊谢尔伦

Create a NSOperationQueue ,设置其 maxConcurrentOperationCount1 so that they are executed sequentially.

queue.addOperationWithBlock {
    manager.request(.GET, ...)
}
小葫芦

Okay, let me post the code

If the network request itself is asynchronous, then joining the ready-made queue will definitely be asynchronous directly. Directly executing the next one cannot be directly queued for execution.

So you have two methods to handle it. One is to block with a signal, and the other is to call back the next operation after completion. In short, it is similar to

 NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 
    //your request
}];

Then put all the operations into an operations array

Then

 NSOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ 
    //your request
    // success in your request--> callback next operation
    // fail in your request-->do something
}];

Signal blocking can also be used, but the efficiency will be compromised. It should be easy to understand when written like this, right?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template