Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WKBackgroundRefreshTask being dealloc before its returned

I'm having trouble with my WatchKit app crashing in WatchOS 5. This is happening in the background when my system calls my app to let me update the complication.

The code snippet here is called in the background when I get a WKApplicationBackgroundRefreshTask:

    func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
    for task in backgroundTasks {
        switch task {
        case let backgroundTask as WKApplicationRefreshBackgroundTask:
                HealthController().updateComplicationData {
                    self.updateComplications()
                    if #available(watchOSApplicationExtension 4.0, *) {
                        backgroundTask.setTaskCompletedWithSnapshot(false)
                    } else { task.setTaskCompleted()}
            }

The issue is, the crash I get basically says:

[WKRefreshBackgroundTask dealloc]

So I'm aware, this could be happening if the HealthController().updateComplicationData() function takes too long to return. But the crash doesn't imply the app is being killed for cpu time issues. It seems that the actual backgroundTask variable is getting deallocated so that when the backgroundTask.setTaskCompletedWithSnapshot(false) call happens I pass a deallocated object.

Any suggestions?

  1. Could I somehow make the backgroundTask variable in a way that it doesn't get deallocated?
  2. Could I be wrong about the cause of the crash and I'm missing something else?
  3. Am I just forced to find a way to reduce the load that the HealthController().updateComplicationData creates?

Thanks for your help!

like image 247
Simon Avatar asked Jul 07 '26 05:07

Simon


1 Answers

I have finally solved this incase anyone else experiences it! Basically, I had an odd pathline of code in the background refresh where occasionally the finishedBackgroundUpdate handler wasnt called. Because of this WatchKit was killing the app. All solved now though.

like image 121
Simon Avatar answered Jul 10 '26 03:07

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!