PromiseKit 3.0:用循环链接

时间:2015-11-01 18:51:20

标签: swift2 promisekit

我使用promisekit 3.0以干净的方式帮助链接alamofire回调。目标是从网络调用开始,承诺返回一组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
}

有人可以告诉我如何正确地构建这个吗?

是否可以避免像反模式一样嵌套承诺?在这种情况下,最好的方法是什么。

2 个答案:

答案 0 :(得分: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
    }

我唯一没有做的就是在这个示例中显示保留接收到的数据,但我假设您可以使用现在的结果数组执行此操作。

答案 1 :(得分:0)

解决我的特定问题的关键是使用“when”功能。它一直持续到你输入的所有电话都完成了。地图使人们更容易看到(并在脑海中思考)

 }.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 { 
        }
 }
相关问题