我試圖在返回之前鏈接一些需要解決的承諾。PromiseKit上的鏈接承諾
在我的情況下,對於databaseResult的每個元素,我需要使用返回promise的方法獲取一些數據。
一旦我爲數組的每個單元獲取數據,我需要返回到調用方法。
var toReturn = [MatchModel]()
//get my array of data
let databaseResults = MatchDatabaseManager.getMatchList();
//not sure what I'm doing
var promise = dispatch_promise{ 0 }
if(databaseResults.count > 0) {
return Promise { fulfill, reject in
for index in 0..<databaseResults.count {
print(index)
promise = promise.then { y -> Promise<Int> in
//Fetch the data I need ...
DataProvider.getUserProfileWithUserId(
(databaseResults[y].partnerUserProfile?.userId)!)
.then {(model) in {
//and use it to create the data I need to return
toReturn.append(MatchModel(realmModel:
databaseResults[y], partnerProfile: model))
}
}
return dispatch_promise { index }
}
}
//Once all the promises are fulfilled, toReturn contains the data I need and I can return it
promise.then{ x in {
fulfill(toReturn)
}
}
}
}
如果我運行此我得到
PromiseKit: Pending Promise deallocated! This is usually a bug
我有PromiseKit和文檔/ exaples是稀缺的經驗非常少,所以我不知道我在這裏失蹤。