【问题标题】:PromiseKit 3.0: chaining with loopsPromiseKit 3.0:用循环链接
【发布时间】:2023-03-29 09:16:01
【问题描述】:

我正在使用 promisekit 3.0 来帮助以一种干净的方式链接 alamofire 回调。目标是从网络调用开始,并承诺返回一个 url 数组。

然后,我希望根据需要对这些 url 执行网络调用,以找到我正在寻找的下一个链接。找到此链接后,我可以将其传递到下一步。

这部分是我卡住的地方。

我可以在数组中选择一个我知道有我想要的索引的任意索引,但在返回正确的信息之前,我无法弄清楚循环以使其继续运行。

我尝试从这个 obj-c 示例中学习,但我无法让它在 swift 中运行。

https://stackoverflow.com/a/30693077/1079379

他是我所做工作的一个更具体的例子。

Network.sharedInstance.makeFirstPromise(.GET, url: NSURL(string: fullSourceLink)! )
.then {  (idArray) -> Promise<AnyObject> in
    let ids = idArray as! [String]

    //how do i do that in swift? (from the example SO answer)
    //PMKPromise *p = [PMKPromise promiseWithValue: nil]; // create empty promise

    //only thing i could do was feed it the first value
    var p:Promise<AnyObject> = Network.sharedInstance.makePromiseRequestHostLink(.POST, id: ids[0])

    //var to hold my eventual promise value, doesn't really work unless i set it to something first
    var goodValue:Promise<AnyObject>

    for item in ids {
        //use continue to offset the promise from before the loop started
        continue

        //hard part
        p = p.then{ returnValue -> Promise<AnyObject> in

            //need a way to check if what i get is what i wanted then we can break the loop and move on
            if returnValue = "whatIwant" {
                goodvalue = returnValue
                break
            //or else we try again with the next on the list
            }else {
                return Network.sharedInstance.makeLoopingPromise(.POST, id: item)
            }
        }
    }
    return goodValue
}.then { (finalLink) -> Void in
    //do stuck with finalLink
}

有人可以告诉我如何正确地构造它吗?

要避免嵌套 promise 这样的反模式吗?在这种情况下,最好的方法是什么。

【问题讨论】:

    标签: swift2 promisekit


    【解决方案1】:

    结合您的帖子和您发布的链接,我终于弄清楚了这一点。它有效,但如果有人对正确的解决方案提出意见,我会很高兴。

    func download(arrayOfObjects: [Object]) -> Promise<AnyObject> {
    
            // This stopped the compiler from complaining
            var promise : Promise<AnyObject> = Promise<AnyObject>("emptyPromise")
    
            for object in arrayOfObjects {
                promise = promise.then { _ in
                    return Promise { fulfill, reject in
                        Service.getData(stuff: object.stuff completion: { success, data in
                            if success {
                                print("Got the data")
                            }
    
                            fulfill(successful)
                        })
                    }
                }
            }
    
            return promise
        }
    

    我唯一没有做的就是在这个例子中展示的是保留接收到的数据,但我假设你可以用你现在拥有的结果数组来做到这一点。

    【讨论】:

    • 我不久前通过使用 when(请参阅我的答案)弄清楚了我的事情。不需要一个空的承诺,我不需要亲自迭代和创建承诺对象。相反,我遍历结果数组。
    【解决方案2】:

    找出我的特定问题的关键是使用“何时”功能。它一直持续到您输入的所有呼叫都完成为止。地图使查看(并在我的脑海中思考)更容易

     }.then {  (idArray) -> Void in
    
            when(idArray.map({Network.sharedInstance.makePromiseRequest(.POST, params: ["thing":$0])})).then{ link -> Promise<String> in
                return Promise { fulfill, reject in
                    let stringLink:[String] = link as! [String]
                    for entry in stringLink {
                        if entry != "" {
                            fulfill(entry)
                            break
                        }
                    }
                }
            }.then { 
            }
     }
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 2021-05-12
      • 2015-12-14
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多