mapbox-maps-ios icon indicating copy to clipboard operation
mapbox-maps-ios copied to clipboard

Completion not being called when using "withCheckedThrowingContinuation"

Open JBergsee opened this issue 2 years ago • 2 comments

Environment

  • Xcode version: 14.2
  • iOS version: 16.3
  • Devices affected: sim and hardware
  • Maps SDK Version: 10.10

Observed behavior and steps to reproduce

Trying to make an async version of the callback-based API in MapBox, using the following code as example:

public func asyncTileRegions() async throws -> [TileRegion] {
        return try await withCheckedThrowingContinuation { continuation in
            TileStore.default.allTileRegions { result in
                
                // depending on the content of result, we either resume with a value or an error
                switch result {
                case .success(let value):
                    value.forEach { region in
                        print("Region: \(region.id), \(region.completedResourceSize.formatted(.byteCount(style: .binary)))")
                    }
                    continuation.resume(returning: value)
                case .failure(let error):
                    continuation.resume(throwing: error)
                }
            }
        }
    }

The callback from allTileRegions does not get called. Console output is: SWIFT TASK CONTINUATION MISUSE: asyncTileRegions() leaked its continuation!

Expected behavior

The callback is executed, and the region id and size is printed.

Notes / preliminary analysis

This works as expected without the async and "withCheckedThrowingContinuation".

JBergsee avatar Mar 16 '23 23:03 JBergsee

@JBergsee Hi there! Try to run the code on @MainActor. We had the same problem and this was our fix.

vladpre92 avatar Mar 25 '23 21:03 vladpre92

Hi @vladpre92 ! Thank you very much for your comment, I appreciate the help! I have tried various ideas now, but without success. Where do you suggest I put @MainActoror MainActor.run {? The function is called from the UI already (running on the main actor), so I have to put the async function call in a Task {. And adding @MainActorwithin the allTileRegionscallback does not seem to work either.

JBergsee avatar Apr 07 '23 10:04 JBergsee