[英]Async/Await function without a return | Swift 5.5
How can I remove the Bool return from my function without getting the error:如何在不出现错误的情况下从 function 中删除 Bool 返回值:
Generic parameter 'T' could not be inferred
This is the function:这是 function:
private func syncDataStore() async throws -> Bool {
try await withUnsafeThrowingContinuation { continuation in
Amplify.DataStore.stop { (result) in
switch(result) {
case .success:
Amplify.DataStore.start { (result) in
switch(result) {
case .success:
print("DataStore started")
continuation.resume(returning: true)
case .failure(let error):
print("Error starting DataStore:\(error)")
continuation.resume(throwing: error)
}
}
case .failure(let error):
print("Error stopping DataStore:\(error)")
continuation.resume(throwing: error)
}
}
}
}
This is what I tried to do but I get the error mentioned above:这是我尝试做的,但出现上述错误:
private func syncDataStore() async throws {
try await withUnsafeThrowingContinuation { continuation in
Amplify.DataStore.stop { (result) in
switch(result) {
case .success:
Amplify.DataStore.start { (result) in
switch(result) {
case .success:
print("DataStore started")
continuation.resume()
case .failure(let error):
print("Error starting DataStore:\(error)")
continuation.resume(throwing: error)
}
}
case .failure(let error):
print("Error stopping DataStore:\(error)")
continuation.resume(throwing: error)
}
}
}
}
Honestly I don't know why it's complaining, no returns are there and it's not tie to any model or anything...老实说,我不知道为什么它会抱怨,那里没有回报,它与任何 model 或任何东西都没有关系......
这是最有效的:
try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Void, Error>) in
Let's look at signature来看看签名
/// Suspends the current task,
/// then calls the given closure with the an unsafe throwing continuation for the current task.
///
/// - Parameter fn: A closure that takes an `UnsafeContinuation` parameter.
/// You must resume the continuation exactly once.
///
/// - Returns: The value passed to the continuation by the closure.
///
/// If `resume(throwing:)` is called on the continuation,
/// this function throws that error.
@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
public func withUnsafeThrowingContinuation<T>(_ fn: (UnsafeContinuation<T, Error>) -> Void) async throws -> T
as it is seen withUnsafeThrowingContinuation
is function with generics on return value which type is detected from continuation正如它所看到的, withUnsafeThrowingContinuation
是返回值泛型的函数,该类型是从延续中检测到的
So the solution for your case can be as follows:因此,您的案例的解决方案如下:
private func syncDataStore() async throws {
_ = try await withUnsafeThrowingContinuation { continuation in
// ...
continuation.resume(returning: true)
// ...
}
}
在你的“private func syncDataStore() async throws {...}”中试试这个:
return try await withUnsafeThrowingContinuation {....}
The trick is to cast return value of withUnsafeThrowingContinuation<\/code> to
Void<\/code> , like so:
诀窍是将
withUnsafeThrowingContinuation<\/code>返回值
withUnsafeThrowingContinuation<\/code>为
Void<\/code> ,如下所示:
try await withUnsafeThrowingContinuation { continuation in
someAsyncFunction() { error in
if let error = error { continuation.resume(throwing: error) }
else { continuation.resume() }
}
} as Void
private func syncDataStore() async throws -> Void {
try await withCheckedThrowingContinuation { continuation in
...
case .success:
continuation.resume()
case .failure(let error):
continuation.resume(throwing: error)
...
}
}
This does work on iOS 14这确实适用于 iOS 14
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.