2016-01-22 27 views
1

我有3個承諾,每一個工作獨立:斯威夫特2 Promisekit 3鏈ASYC方法瘋狂

DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928").then { data in 
    print("Zip \(data)") 
} 

DataManager.getEnviroUVWithZipPromise("48073").then { data in 
    print("UV \(data.getUVIndex())") 
} 

DataManager.getCurrentForZipPromise("48073").then { data in 
    print("AirNow \(data[0].Latitude)") 
} 

我一直在試圖遵循promisekit 3(殆盡谷歌和堆棧溢出)(sparce)迅速文檔。我嘗試鏈接這些異步方法根本不是同步行爲 - 所有打印(數據)都爲空,但方法中的斷點顯示數據。

DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928").then { data in 
    print("Promise Zip \(data)") 
}.then { data -> Void in 
    DataManager.getEnviroUVWithZipPromise("48073") 
    print("Promise Enviro \(data)") 
}.then { data -> Void in 
    DataManager.getCurrentForZipPromise("48073") 
    print("Promise AirNow \(data)") 
}.error { error in 
    print("Promise doh!") 
} 

我希望將第一個調用(zip)的數據傳遞給後續調用,並且能夠處理最後一次調用後的所有調用的數據。任何見解非常感謝!

+0

看看我的答案語法。 http://stackoverflow.com/questions/34501537/ambigous-use-of-recover-error-while-using-promisekit/34502275#34502275 – Jon

回答

0

您的問題是,你的封正在返回Void,而不是Promise<T>then塊不會期望返回任何內容,因此它不會等待承諾被執行或拒絕。

這是給你一個完整的例子。您可以將它粘貼到PromiseKit操場中進行測試。

var zipcode: String? 

UIApplication.sharedApplication().networkActivityIndicatorVisible = true 

firstly { 

    return DataManager.reverseGeoCodePromise("42.527328", longitude: "-83.146928") 

}.then { (data: String) -> Promise<String> in 
    //^I expect to receive a string. I promise to return a string   

    zipcode = data 
    //^Store zip code so it can be used further down the chain 

    print("Returned from reverseGeoCodePromise: \(data)") 
    return DataManager.getEnviroUVWithZipPromise(zipcode!) 

}.then { (data: String) -> Promise<String> in 
    //^I expect to receive a string. I promise to return a string 

    print("Returned from getEnviroUVWithZipPromise: \(data)") 
    return DataManager.getCurrentForZipPromise(zipcode!) 

}.then { (data: String) -> Void in 
    //^I expect to receive a string. I return nothing 

    print("Returned from getCurrentForZipPromise: \(data)") 

}.always { 

    UIApplication.sharedApplication().networkActivityIndicatorVisible = false 

}.error { error in 

    switch error { 
    case DataManagerError.FailedDueToX: 
     print("got an error") 
    default: 
     print("unknown error") 
    } 

} 

例的自定義錯誤類型:

enum DataManagerError: ErrorType { 
    case FailedDueToX 
} 

實例數據管理的承諾:在這裏

class DataManager { 
    static func reverseGeoCodePromise(lat: String, longitude: String) -> Promise<String> { 
     return Promise { fulfill, reject in 
      if 1 == 1 { 
       fulfill("48073") 
      } else { 
       reject(DataManagerError.FailedDueToX) 
      } 
     } 
    } 

    static func getEnviroUVWithZipPromise(zip: String) -> Promise<String> { 
     return Promise { fulfill, reject in 
      if 1 == 1 { 
       fulfill("Two") 
      } else { 
       reject(DataManagerError.FailedDueToX) 
      } 
     } 
    } 

    static func getCurrentForZipPromise(zip: String) -> Promise<String> { 
     return Promise { fulfill, reject in 
      if 1 == 1 { 
       fulfill("Three") 
      } else { 
       reject(DataManagerError.FailedDueToX) 
      } 
     } 
    } 

} 
+0

請注意,您不必首先使用'first',我只是更喜歡它看起來如何。 – Jon

+0

謝謝!事實上,兩個方法返回的自定義對象,而不是字符串...張貼在這裏: –

+0

我使用的字符串只是用於說明目的。您可以用數據類型進行替換以適應您的確切場景。關於您的郵政編碼評論,您可以將其存儲在承諾鏈之外。我會更新我的答案,告訴你如何。 – Jon