Home IOS Development ios – SwiftUI: Widget replace lagging by one replace

ios – SwiftUI: Widget replace lagging by one replace

0
ios – SwiftUI: Widget replace lagging by one replace

[ad_1]

Background: I’ve carried out a widget in a SwiftUI venture. The primary app permits folks to see bus service schedules in any respect bus stops. They’ll ‘favorite’ buses at stops they use regularly. These favourites are saved regionally utilizing SwiftData. The widget permits customers to view the schedule of the highest 4 favorite buses within the widget.

I’ve carried out it such that each time there’s a context.insert or context.delete in the primary app, I subsequently name WidgetCenter.shared.reloadAllTimelines() in order that the widgets content material is up to date with the most recent favourites.

I’ve used an asynchronous Process throughout the physique of the widget Timeline supplier to get the entry and replace the timeline.

Code inside timeline supplier:

@MainActor
func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
   
    
    Process{
        
        //1.Get from Core Knowledge
        let faveServicesSaved =   getFavourites()
        print("Widget: Get Favourites Triggered (faveServicesSaved.rely)")
        
        //Get arrival information
        let faveServicesReturned = attempt? await fetchAllData(providers: faveServicesSaved)
        print("Widget: Arrival Knowledge Retrieved (faveServicesReturned?.rely)")
        
        //Kind by location
        let faveServicesSorted = sortByLocation(faveServicesReturned ?? [])
        print("Widget: Sorted by location (faveServicesSorted.rely)")
        
        let nearestFaveServices = faveServicesSorted.prefix(4)
        print("Widget: Nearest fave providers (nearestFaveServices.rely)")
        
        //select the primary 4 for show
        let entry = Favourites(date: Date(), providers: Array(nearestFaveServices))
        
        
        let timeline = Timeline(entries: [entry], coverage: .by no means)
        completion(timeline)
    }
}

//Since can not entry @Question from inside Timeline supplier, must get information utilizing Core Knowledge
@MainActor
non-public func getFavourites() ->[FaveService]{
    
    guard let container = attempt? ModelContainer(for: FaveService.self) else {
        print("Widget: Drawback with container setup")
        return []
    }
    
    let descriptor = FetchDescriptor<FaveService>()
    let faves = attempt? container.mainContext.fetch(descriptor)
    print("Widget (faves?.rely)")
    return faves ?? []
}

Drawback: I discover that the replace to widget content material is lagging by one replace i.e. the primary time I add a favorite from the primary app it does not seem within the content material. After I add the second favorite, the primary one seems. After I add a 3rd favorite, the second seems. and so forth.

What I’ve tried:

  • As I discussed, throughout the physique of the primary app I’ve double checked to make sure that reloadAllTimelines is being known as after a context.insert.
  • I’ve checked to make sure that all code is working on the primary thread so there are no updating conflicts.
  • I’ve checked that the mannequin container is appropriately setup for the primary pull
  • I’ve tried different replace insurance policies together with .atEnd and .after(now.advance(by: 60)).

Any concepts what may be inflicting this lagged replace?

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here