- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 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");
});
});
}
最佳答案
您正在寻找的概念是then
able 链接。你想在一个 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/
我正在使用 swift 3 在 Xcode 8.2.1 上开发一个应用程序。我有一些异步任务需要按适当的顺序排列,因此我决定为此目的使用 PromiseKit。我在我的项目目录中添加了一个 podfi
我有一些昂贵的 promise ,在不同的地方被调用。理想情况下,我只想链接现有的飞行 promise (使用可选的强制),所以我发现自己在做这样的事情: class Expensive { va
ReactiveSwift具有称为flatMapError的强大功能,允许您在发生错误时以事件流进行响应。一个简单的示例可能如下所示: authenticationProducer.flatMapEr
我正在将 PromiseKit 集成到我们当前的系统中,我需要链的 catch 部分使用 2 个参数。我希望我的 catch 使用 error, fetcher 而不仅仅是错误。执行此操作的最佳方法是
刚开始在 Swift 中使用 PromiseKit 6。我检查了一些示例并阅读了文档。我有一个看似微不足道的场景,但我无法绕过它。我有以下场景: func addModel(_ model: Mode
刚开始使用 PromiseKit 遇到了一个奇怪的问题编译问题: with firSTLy:对成员“firSTLy(execute:)”的引用不明确 () without firSTLy:无法推断复杂
我是编程新手。因此,安装 PromiseKit 对我来说一直很困难。在研究了各种安装方法后,我尝试通过 Carthage 安装(结果没有成功,因为我不得不弄清楚如何安装 Carthage,但我从来没能
我的应用程序中有一系列 UIViewController(弹出窗口),用于从最终用户那里收集信息。我设法用这样的 promise 将它们链接起来: firstly { return s
我目前正在混合使用 SwiftyStoreKit 和 PromiseKit 以处理应用内购买,我遇到的问题/问题是在 promise 链中,如果我在其中一个中抛出错误,则 catch block 不是
我正在使用 PromiseKit 来处理我的网络调用。我正在尝试查看是否有约定或更简洁的方法来尽早履行或拒绝 promise 。如下图所示,有一些条件需要我提前满足或拒绝。我目前通过在之后放置一个 r
我如何进行可选 promise 链接?例如,我有两个 promise ,我必须仅根据第一个 promise 结果的某些条件来检查第二个 promise 。 promise1(). then { res
假设您的 Promise 链中有一个分支,它可以不返回任何内容,也可以返回 AnyObject Promise。您将指定什么作为“then”闭包的返回类型?例如: func sample() -> P
我已经开始在异步请求中使用 PromiseKit,但现在遇到一个问题,我无法解析链中的错误。我有一个请求管理器,其方法如下: func request( url: String,
我正在使用 PromiseKit(很棒的框架,顺便说一句!)来处理我的应用程序和服务器 API 之间的通信。我想创建某种全局处理程序来响应诸如通知用户我的许多 promise 中的任何一个缺乏网络连接
我可以在 AppDelegate - application:didReceiveRemoteNotification 方法中使用 PromiseKit 吗? 我正在使用 GCM 从服务器推送通知。一
使用 Swift,我第一次尝试使用 PromiseKit 将 promises 合并到我的代码中,但我很难从语法上理解如何实现它。 我的目标是通过管道将请求传递给 Firebase 以获取数据,并在附
我试图在 promise 工具包上链接一些 promise,当 promise 类型像这样时我遇到语法问题 Promise ,只有当 promise 有类型时,我才会收到编译器错误。我刚开始使用 pr
我正在尝试使用 PromiseKit,但在解决这个问题时遇到了一些麻烦。我有一个函数可以做类似的事情 func lengthyOperation() -> Promise { return Pr
PromiseKit 提供了一个方便的方法 thenOn 用于在非主线程上运行你的部分链。然而,似乎没有任何方便的方法来设置第一个 promise 执行线程。 这意味着我最终要么在我的第一个 prom
我有下一个递归 Promise 函数调用: func getPlaces(location: Location) -> Promise { return Promise().then {
我是一名优秀的程序员,十分优秀!