swiftasync-awaitwidgetkitweatherkit

Weather API + WidgetKit - "'async' call in a function that does not support concurrency"


I'm trying to make a TimelineProvider for iOS 16 WeatherKit and I'm struggling with how I meet the protocol signature and and use the new async api.

Here's my code:

struct WeatherProvider: TimelineProvider {
    
    func getSnapshot(in context: Context, completion: @escaping (WeatherEntry) -> ()) {       
        let weather = try? await WeatherService.shared.weather(for: currentLocation)        
        let entry = WeatherEntry(date: Date(), wind: nil, locationAuthStatus: nil)

        completion(entry)
    }

    // etc
}

Compiling this leads throws a build error 'async' call in a function that does not support concurrency.

The auto fix suggests: Add 'async' to function 'getSnapshot(in:completion:)' to make it asynchronous

But if you do that the struct no longer conforms: Type 'WeatherProvider' does not conform to protocol 'TimelineProvider'

This feels like it should be trivial but I can't find any examples on how to do it.


Solution

  • Just put everything in a Task and you are good to go. Task is a good way to bridge completion handler syntax to the new async/await, or more general execute async code in a synchronous context:

    Task{
    
        let weather = try? await WeatherService.shared.weather(for: currentLocation)        
        let entry = WeatherEntry(date: Date(), wind: nil, locationAuthStatus: nil)
        
        completion(entry)
    }