0

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

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

这部分是我卡住的地方。

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

我尝试从这个 obj-c 示例中学习,但我无法让它快速工作。

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
}

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

嵌套承诺是否应该避免这种反模式?在这种情况下,最好的方法是什么。

4

2 回答 2

2

我终于通过结合您的帖子和您发布的链接来解决这个问题。它有效,但如果有人对适当的解决方案提出意见,我会很高兴。

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
    }

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

于 2015-11-24T19:40:17.140 回答
0

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

 }.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 { 
        }
 }
于 2015-12-08T00:59:10.950 回答