swift 异步/等待函数,不返回|雨燕5.5

s8vozzvw  于 2023-01-16  发布在  Swift
关注(0)|答案(5)|浏览(124)

我怎样才能从我的函数中删除Bool返回而不出现错误:

Generic parameter 'T' could not be inferred

这是一个函数:

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)
            }
        }
    }
}

这是我试图做的,但我得到上面提到的错误:

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)
            }
        }
    }
}

老实说,我不知道它为什么抱怨,没有回报,它不依赖于任何模型或任何东西...

xxb16uws

xxb16uws1#

这是最有效的方法:

try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation<Void, Error>) in
pbwdgjma

pbwdgjma2#

技巧是将withUnsafeThrowingContinuation的返回值强制转换为Void,如下所示:

try await withUnsafeThrowingContinuation { continuation in
    someAsyncFunction() { error in
        if let error = error { continuation.resume(throwing: error) }
        else { continuation.resume() }
    }
} as Void
vsnjm48y

vsnjm48y3#

让我们看看签名

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

如图所示,withUnsafeThrowingContinuation是返回值具有泛型的函数,其类型从continuation中检测到
因此,您的情况下的解决方案可以如下:

private func syncDataStore() async throws {
    _ = try await withUnsafeThrowingContinuation { continuation in

        // ...

        continuation.resume(returning: true)

        // ...
    }
}
n9vozmp4

n9vozmp44#

在“私有函数syncDataStore()异步抛出{...}"中尝试以下操作:

return try await withUnsafeThrowingContinuation {....}
kulphzqa

kulphzqa5#

private func syncDataStore() async throws -> Void {
    try await withCheckedThrowingContinuation { continuation in
        ...
        case .success:
            continuation.resume()
        case .failure(let error):
            continuation.resume(throwing: error)
        ...
    }
}

这在iOS 14上确实有效

相关问题