Sunday, April 14, 2024
HomeiOS Developmentios - Can I deal with a BGProcessingTask with SwiftUI's backgroundTask(_:) scene...

ios – Can I deal with a BGProcessingTask with SwiftUI’s backgroundTask(_:) scene modifier?


I am engaged on an app that should course of cached location information within the background periodically. Whereas the duty often completes in lower than 30 seconds, it might probably be battery- and network-intensive, because it reverse-geocodes a number of cached areas. And because the activity might be thought-about “database upkeep”, I wish to implement it as a background processing activity (BGProcessingTask).

I’m making an attempt to make use of SwiftUI’s backgroundTask(_:) scene modifier to deal with the duty. I’ve watched the “Effectivity Awaits” WWDC video (https://developer.apple.com/wwdc22/10142) and browse the documentation, and I’ve not discovered any details about learn how to deal with BGProcessingTasks. BackgroundTask supplies appRefresh and urlSession duties, however not background processing duties.

I’ve tried registering a BGProcessingTaskRequest, after which dealing with it with .appRefresh. A simplified model of my code seems to be like this:

struct MyApp: App {
    @Atmosphere(.scenePhase) personal var scenePhase

    var physique: some Scene {
        WindowGroup {
            ContentView()
        }
        .onChange(of: scenePhase) { _, newValue in
            if case .background = newValue {
                scheduleBackgroundTasks()
            }
        }
        .backgroundTask(.appRefresh("com.MyCompany.MyApp-ProcessLocations")) {
            scheduleBackgroundTasks()
            do {
                attempt await AppDataStore.processLocations()
            } catch {
                print(error)
            }
        }
    }
}

func scheduleBackgroundTasks() {
    let taskDate = nextTaskDate()
    let bgTaskRequest = BGProcessingTaskRequest(
        identifier: "com.MyCompany.MyApp-ProcessLocations"
    )
    bgTaskRequest.earliestBeginDate = taskDate
    bgTaskRequest.requiresExternalPower = true
    bgTaskRequest.requiresNetworkConnectivity = true
    attempt? BGTaskScheduler.shared.submit(bgTaskRequest)
}

The duty efficiently runs when triggering it with the debugging technique used within the WWDC movies, however since I am unable to discover any revealed information on the topic, I do not know if it is protected to do that in manufacturing.

What are the implications of dealing with the processing activity with .appRefresh? For instance will the system permit my app to run for the period of time allotted to processing duties, or will or not it’s terminated after 30 seconds like an app refresh activity? Will the backgroundTask modifier accurately inform the system when the duty completes?

Lastly, if the backgroundTask modifier cannot be used, how ought to background processing duties be dealt with in a SwiftUI app?



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments