Reputation: 930
How can I remove the Bool return from my function without getting the error:
Generic parameter 'T' could not be inferred
This is the 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...
Upvotes: 14
Views: 6737
Reputation: 1
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
Upvotes: -1
Reputation: 331
The trick is to cast return value of withUnsafeThrowingContinuation
to Void
, like so:
try await withUnsafeThrowingContinuation { continuation in
someAsyncFunction() { error in
if let error = error { continuation.resume(throwing: error) }
else { continuation.resume() }
}
} as Void
Upvotes: 9
Reputation: 930
This is what worked the best:
try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Void, Error>) in
Upvotes: 8
Reputation: 257729
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
So the solution for your case can be as follows:
private func syncDataStore() async throws {
_ = try await withUnsafeThrowingContinuation { continuation in
// ...
continuation.resume(returning: true)
// ...
}
}
Upvotes: 1
Reputation: 36323
try this in your "private func syncDataStore() async throws {...}":
return try await withUnsafeThrowingContinuation {....}
Upvotes: 0