Kim*_*Kim 7 swift promisekit swift5 ios15
我是 Swift 的新手,来自 JS,我已经开始构建一个 iOS 应用程序。
最初,我使用 Promise Kit 来处理异步内容,因为它对我来说比我读到的其他内容更容易。
无论如何,在 JS 中,我经常使用以下模式:
async function doAyncFunction(item) {
try {
// do async call to fetch data using item
return Promise.resolve(data);
} catch (error) {
return Promise.reject(error);
}
}
const promises = items.map((item) => doAyncFunction(item));
const results = await Promise.all(promises);
Run Code Online (Sandbox Code Playgroud)
我最终通过 Promise Kit 实现了这一点,如下所示:
func doManyAsyncRequests(userIds: [String], accessToken: String) -> Promise<Void> {
Promise { seal in
let promises = spotifyUserIds.map {
doSingleAsyncRequest(userId: $0.id, accessToken: accessToken) // this function returns a promise
}
when(fulfilled: promises).done { results in
print("Results: \(results)")
// process results
}.catch { error in
print("\(error)")
// handle error
}
}
}
Run Code Online (Sandbox Code Playgroud)
Promise Kitwhen
与 JavaScript 类似Promise.all()
,一旦实现了 Promise,就会触发代码中的操作。
由于我的学习曲线足够慢,我决定开始为 iOS 15 编码并使用 Swift async/await。
问题:哪种 Swift 异步/等待模式可以完成上述操作,类似于 Promise Kitwait
和 JavaScript Promise.all()
?
谢谢。
更新:感谢@workingdog,他帮助我找到了下面的解决方案。我现在必须处理错误,但现在这是一个不同的主题。
func getAllThings(users: [User], accessToken: String) async -> [Thing] {
var allThings: [Thing] = []
await withTaskGroup(of: [Thing].self) { group in
for user in users {
group.async {
let userThings = await self.getUsersThings(
accessToken: accessToken,
displayName: user.displayName,
userId: user.id
)
return userThings
}
}
for await (userThings) in group {
allThings = allThings + userThings
}
}
return allThings
}
Run Code Online (Sandbox Code Playgroud)
您可能正在寻找withTaskGroup(...)
,例如:
func getAll() async {
await withTaskGroup(of: Void.self) { group in
await getPosts()
for post in posts {
group.async { await self.getCommentsFor(post: post) }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在 github 上设置了自己的基本测试来学习这一点: https: //github.com/workingDog/TestAsync
编辑:
这就是我返回一系列带有评论的帖子的方式。正如您所看到的,它不如 getAll() 简洁。
func getAllPosts() async -> [Post] {
// this is the tricky parameter bit, the tuple returned when you call group.async {...}
await withTaskGroup(of: (Int, [Comment]).self) { group in
// get all the posts
var thePosts: [Post] = await fetchThem()
// for each post get all the comments (concurrently)
for post in thePosts {
group.async {
let comments: [Comment] = await self.fetchThem(with: post)
return (post.id, comments)
}
}
// add the comments to their corresponding post (concurrently)
for await (postid, comnts) in group {
if let ndx = thePosts.firstIndex(where: {$0.id == postid}) {
thePosts[ndx].comments = comnts
}
}
// when all done, return all post with their comments all cooked up
return thePosts
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3114 次 |
最近记录: |