• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

Assertion failure in -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:]

iOS cps12345 8个月前 (12-02) 277次浏览 0个评论

我正在使用BGTaskScheduler API在我的iOS 14应用程序中注册后台任务,该应用程序使用新应用程序@Main代替AppDelegate。我以为我们将使用scenePhase以下方法来模仿AppDelegate中didFinishLaunching的先前功能,但以下方法会导致崩溃:*** Assertion failure in -[BGTaskScheduler _unsafe_registerForTaskWithIdentifier:usingQueue:launchHandler:], BGTaskScheduler.m:185 2020-12-01 12:53:40.645091-0500 newFitnessApp[13487:1952133] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘All launch handlers must be registered before application finishes launching’如何实现BGTaskScheduler?

@main
struct newFitnessAppApp: App {
    
    @Environment(\.scenePhase) var scenePhase
    
    //Launch Count (for requesting reviews and to show tutorial)
    //var launchCount = UserDefaults.standard.integer(forKey: TrackerConstants.launchCountKey)
    //var testMode = true
    //@Environment(\.scenePhase) private var phase
    let trackerDataStore = TrackerDataStore(workoutLoader: HealthKitWorkoutLoader())
    
    var body: some Scene {
        WindowGroup {
            AppRootView().environmentObject(trackerDataStore)
        }
        .onChange(of: scenePhase) { newScenePhase in
            switch newScenePhase {
            case .active:
                print("App is active")
                registerBackgroundTasks()

解决

在 init中注册

@main
struct newFitnessAppApp: App {

   init() {
      registerBackgroundTasks()
   }

// ... other code
}
喜欢 (0)

您必须 登录 才能发表评论!