简体   繁体   中英

Async/Await function without a return | Swift 5.5

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...

这是最有效的:

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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM