使用PromiseKit强制顺序下载

我正在使用PromiseKit,并希望强制顺序下载JSON。 JSON的数量可能会改变。

我读过关于链接的内容。 如果我有3个固定数量的下载,那就没关系。

但是,如果我想要按顺序下载的下载次数变化怎么办?

这是我的2个URL的代码。 我想知道如何使用dateUrlArray[i]迭代数组来做到这一点?

  - (void)downloadJSONWithPromiseKitDateArray:(NSMutableArray *)dateUrlArray { [self.operationManager GET:dateUrlArray[0] parameters:nil] .then(^(id responseObject, AFHTTPRequestOperation *operation) { NSDictionary *resultDictionary = (NSDictionary *) responseObject; Menu *menu = [JsonMapper mapMenuFromDictionary:resultDictionary]; if (menu) { [[DataAccess instance] addMenuToRealm:menu]; } return [self.operationManager GET:dateUrlArray[1] parameters:nil]; }).then(^(id responseObject, AFHTTPRequestOperation *operation) { NSDictionary *resultDictionary = (NSDictionary *) responseObject; Menu *menu = [JsonMapper mapMenuFromDictionary:resultDictionary]; if (menu) { [[DataAccess instance] addMenuToRealm:menu]; } }) .catch(^(NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ [self handleCatchwithError:error]; }); }).finally(^{ dispatch_async(dispatch_get_main_queue(), ^{ DDLogInfo(@".....finally"); }); }); } 

您正在寻找的概念是可以链接的。 您希望在for循环中链接多个promise。

我的Objective-C真的很生疏 – 但看起来应该是这样的:

 // create an array for the results __block NSMutableArray *results = [NSMutableArray arrayWithCapacity:[urls count]]; // create an initial promise PMKPromise *p = [PMKPromise promiseWithValue: nil]; // create empty promise for (id url in urls) { // chain p = p.then(^{ // chain the request and storate return [self.operationManager GET:url parameters:nil].then(^(id responseObject, AFHTTPRequestOperation *operation) { [results addObject:responseObject]; // reference to result return nil; }); }); } p.then(^{ // all results available here }); 

对于我们这些寻找Swift 2.3解决方案的人:

 import PromiseKit extension Promise { static func resolveSequentially(promiseFns: [()->Promise]) -> Promise? { return promiseFns.reduce(nil) { (fn1: Promise?, fn2: (()->Promise)?) -> Promise? in return fn1?.then({ (_) -> Promise in return fn2!() }) ?? fn2!() } } } 

请注意,如果promises数组为空,则此函数返回nil

使用示例

以下是如何按顺序上传附件数组的示例:

 func uploadAttachments(attachments: [Attachment]) -> Promise { let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise) in return { return self.uploadAttachment(attachment) } }) return Promise.resolveSequentially(promiseFns)?.then({}) ?? Promise() } func uploadAttachment(attachment: Attachment) -> Promise { // Do the actual uploading return Promise() } 

感谢Vegard的回答,我为Swift 3重写:

 extension Promise { static func resolveSequentially(promiseFns: [()->Promise]) -> Promise? { return promiseFns.reduce(nil) { (fn1: Promise?, fn2: (()->Promise)?) -> Promise? in return fn1?.then{ (_) -> Promise in return fn2!() } ?? fn2!() } } } /* Example */ func uploadAttachments(_ attachments: [Attachment]) -> Promise { let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise) in return { return self. uploadAttachment(attachment) } }) return Promise.resolveSequentially(promiseFns: promiseFns)?.then{Void -> Void in} ?? Promise { Void -> Void in } }