Swift continuation 不会让 await 继续
Swift continuation doesn't make await continue
我正在使用 async/await 和 CloudKit,我想查询项目。所以我写了以下内容:
private func getItems() async throws -> [Item] {
// let predicate = NSPredicate(value: true)
let predicate = NSPredicate(format: "id != %@", "blah")
let sort = NSSortDescriptor(key: "creationDate", ascending: false)
let query = CKQuery(recordType: "Item", predicate: predicate)
query.sortDescriptors = [sort]
print("Let's go!")
let items: [Item] = await withCheckedContinuation { continuation in
let op = CKQueryOperation(query: query)
op.recordMatchedBlock = { (recordId, result) in
print("recordMatchedBlock")
}
op.queryResultBlock = { result in
print("queryCompletionBlock: Jobs done!")
continuation.resume(returning: [])
}
db.add(op)
}
print("All done!")
return items
}
如果我使用 true-predicate,它会让我在 recordName(我在 Item 上没有的字段)丢失时失败,因此另一个谓词。
无论如何,这会打印“我们走吧!”很好,为每个项目打印“recordMatchedBlock”,然后是“Jobs done!”。那时应该继续继续,并且项目应该获得 [] 值。但是“都完成了!”从不打印。
为什么是“全部完成!”从未打印过?为什么continuation不让await继续?
The async Task APIs in macOS, watchOS, and tvOS differ from the async
Task APIs in iOS. As a result, Task API calls on Actor objects in
multi-platform projects may cause async functions to hang at a
suspension point. (79378627)
就是这样!这完全符合要求。所以现在我将继续在 iOS 上开发它,并希望解决挂起问题。
我正在使用 async/await 和 CloudKit,我想查询项目。所以我写了以下内容:
private func getItems() async throws -> [Item] {
// let predicate = NSPredicate(value: true)
let predicate = NSPredicate(format: "id != %@", "blah")
let sort = NSSortDescriptor(key: "creationDate", ascending: false)
let query = CKQuery(recordType: "Item", predicate: predicate)
query.sortDescriptors = [sort]
print("Let's go!")
let items: [Item] = await withCheckedContinuation { continuation in
let op = CKQueryOperation(query: query)
op.recordMatchedBlock = { (recordId, result) in
print("recordMatchedBlock")
}
op.queryResultBlock = { result in
print("queryCompletionBlock: Jobs done!")
continuation.resume(returning: [])
}
db.add(op)
}
print("All done!")
return items
}
如果我使用 true-predicate,它会让我在 recordName(我在 Item 上没有的字段)丢失时失败,因此另一个谓词。
无论如何,这会打印“我们走吧!”很好,为每个项目打印“recordMatchedBlock”,然后是“Jobs done!”。那时应该继续继续,并且项目应该获得 [] 值。但是“都完成了!”从不打印。
为什么是“全部完成!”从未打印过?为什么continuation不让await继续?
The async Task APIs in macOS, watchOS, and tvOS differ from the async Task APIs in iOS. As a result, Task API calls on Actor objects in multi-platform projects may cause async functions to hang at a suspension point. (79378627)
就是这样!这完全符合要求。所以现在我将继续在 iOS 上开发它,并希望解决挂起问题。