2021-06-27

SWIFT TASK CONTINUATION MISUSE: leaked its continuation - for delegate?

I'm trying to extend my class with async/await capabilities, but at run-time there is an error in the console:

SWIFT TASK CONTINUATION MISUSE: query(_:) leaked its continuation!

Below is the class I'm trying to add the continuation to which uses a delegate:

class LocalSearch: NSObject, MKLocalSearchCompleterDelegate {
    private let completer: MKLocalSearchCompleter
    private var completionContinuation: CheckedContinuation<[MKLocalSearchCompletion], Error>?

    init() {
        completer = MKLocalSearchCompleter()
        super.init()
        completer.delegate = self
    }

    func query(_ value: String) async throws -> [MKLocalSearchCompletion] {
        try await withCheckedThrowingContinuation { continuation in
            completionContinuation = continuation

            guard !value.isEmpty else {
                completionContinuation?.resume(returning: [])
                completionContinuation = nil
                return
            }

            completer.queryFragment = value
        }
    }

    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        completionContinuation?.resume(returning: completer.results)
        completionContinuation = nil
    }

    func completer(_ completer: MKLocalSearchCompleter, didFailWithError error: Error) {
        completionContinuation?.resume(throwing: error)
        completionContinuation = nil
    }
}

This is how I use it:

let localSearch = LocalSearch()

do {
    let results = try await localSearch.query("toront")
    print(results)
} catch {
    print(error)
}

What am I doing wrong or is there a better way to achieve this?



from Recent Questions - Stack Overflow https://ift.tt/3xUSr1P
https://ift.tt/eA8V8J

No comments:

Post a Comment