Simon
Simon

Reputation: 374

watchOS 2 HealthKit HKSampleQuery Crash

So I am having problems debugging a crash on a distributed app. I cannot reproduce this crash personally. However, it seems that a large number of users are having an issue. I suspect it is do with the HKSampleQuery and possibly a return of no results. But I don't understand why no results would return when many users have said they have allowed my app access to their heart rate data. And then even if there we're no results, it should just run the method 'displayError()' but it crashes....

The Code Running In Apple Watch Extension Interface Controller

func loadHealthKitData(){
    NSLog("Loading HealthKit Data")
    let dataTypesToRead = NSSet(object: HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!)
    healthKitStore.requestAuthorizationToShareTypes(nil, readTypes:dataTypesToRead as? Set<HKObjectType>) { (success, error) -> Void in}
    let sampleType = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)!
    let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let date = cal.startOfDayForDate(NSDate())
    let mostRecentPredicate = HKQuery.predicateForSamplesWithStartDate(date, endDate:NSDate(), options: .None)
    let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)

    runHKSampleQuery(sampleType, mostRecentPredicate: mostRecentPredicate, sortDescriptor: sortDescriptor)
}

func runHKSampleQuery(sampleType: HKSampleType, mostRecentPredicate: NSPredicate, sortDescriptor: NSSortDescriptor){
    let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: 1000, sortDescriptors: [sortDescriptor]){
        (sampleQuery, results, error ) -> Void in
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            ****if(results == nil){
                self.displayError()
                return
            }
            self.handleHKResults(results!)
        });
    }
    self.healthKitStore.executeQuery(sampleQuery)
    NSLog("healthKitStore.executeQuery")
}

Symbolised Atos Crash Output in Terminal

InterfaceController.(runHKSampleQuery(HKSampleType, mostRecentPredicate : NSPredicate, sortDescriptor : NSSortDescriptor) -> ()).(closure #1).(closure #1) (in AppleWatch Extension) (InterfaceController.swift:76)

I've added 4 **** at line 76 in the interface controller which is where the crash is supposed to have occurred.

Stack Frame From Crash

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000000e7ffdefe
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   AppleWatch Extension            0x001052f0 0xd8000 + 185072
1   libdispatch.dylib               0x2aa6045a _dispatch_call_block_and_release + 10 (init.c:760)
2   libdispatch.dylib               0x2aa60436 _dispatch_client_callout + 6 (object.m:508)
3   libdispatch.dylib               0x2aa600d0 _dispatch_main_queue_callback_4CF$VARIANT$up + 1480 (inline_internal.h:1063)
4   CoreFoundation                  0x2aedc15e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 10 (CFRunLoop.c:1613)
5   CoreFoundation                  0x2aeda64a __CFRunLoopRun + 1554 (CFRunLoop.c:2718)
6   CoreFoundation                  0x2ae2c90c CFRunLoopRunSpecific + 380 (CFRunLoop.c:2814)
7   Foundation                      0x2b685346 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 258 (NSRunLoop.m:366)
8   Foundation                      0x2b6d0130 -[NSRunLoop(NSRunLoop) run] + 80 (NSRunLoop.m:388)
9   libxpc.dylib                    0x2ac71e76 _xpc_objc_main + 610 (main.m:176)
10  libxpc.dylib                    0x2ac7359a xpc_main + 170 (init.c:1427)
11  Foundation                      0x2b81fa08 -[NSXPCListener resume] + 164 (NSXPCListener.m:257)
12  PlugInKit                       0x339d3950 -[PKService run] + 520 (PKService.m:105)
13  WatchKit                        0x377593be main + 134 (main.m:54)
14  libdyld.dylib                   0x2aaab8ca start + 2 (start_glue.s:64)

Thread 1:
0   libsystem_kernel.dylib          0x2ab9681c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x2ac41a42 _pthread_wqthread + 918 (pthread.c:1999)
2   libsystem_pthread.dylib         0x2ac41698 start_wqthread + 12 (pthread_asm.s:147)

Any help on this would be great. I don't understand why whats happening happens. Cheers

Upvotes: 0

Views: 254

Answers (1)

sschale
sschale

Reputation: 5188

Looking at the point that you think might be the problem:

You might want to change your code to be

if let results = results {
    self.handleHKResults(results)
}else{
    //handle error
}

As a separate issue, you don't seem to be doing anything with the error object, which might help you discover the true issue.

Upvotes: 1

Related Questions