Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async/Await function without a return | Swift 5.5

Tags:

swift

swiftui

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

like image 666
ElKePoN Avatar asked Feb 11 '26 00:02

ElKePoN


1 Answers

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
like image 142
SirDeleck Avatar answered Feb 12 '26 16:02

SirDeleck