我试图在返回之前链接一些需要解决的承诺。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是稀缺的经验非常少,所以我不知道我在这里失踪。