8

我正在使用 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");
        });
    });
}
4

3 回答 3

6

您正在寻找的概念是then能够链接。你想在一个 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
});
于 2015-06-07T11:39:02.513 回答
6

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

import PromiseKit

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

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

使用示例

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

func uploadAttachments(attachments: [Attachment]) -> Promise<Void> {
    let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise<Void>) in
        return {
            return self.uploadAttachment(attachment)
        }
    })
    return Promise.resolveSequentially(promiseFns)?.then({}) ?? Promise()
}

func uploadAttachment(attachment: Attachment) -> Promise<Void> {
    // Do the actual uploading
    return Promise()
}
于 2016-09-27T15:47:48.843 回答
4

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

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



/* Example */
func uploadAttachments(_ attachments: [Attachment]) -> Promise<Void> {
    let promiseFns = attachments.map({ (attachment: Attachment) -> (()->Promise<Void>) in
        return {
            return self. uploadAttachment(attachment)
        }
    })

    return Promise.resolveSequentially(promiseFns: promiseFns)?.then{Void -> Void in} ?? Promise { Void -> Void in }
}
于 2016-11-24T20:35:39.287 回答