gpt4 book ai didi

ios - 使用 PromiseKit 强制顺序下载

转载 作者:可可西里 更新时间:2023-11-01 04:22:03 25 4
gpt4 key购买 nike

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

我已阅读 this关于链接。如果我的下载次数是固定的,比如 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");
});
});
}

最佳答案

您正在寻找的概念是thenable 链接。你想在一个 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
});

关于ios - 使用 PromiseKit 强制顺序下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30692132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com