watchOS 2 HealthKit HKSampleQuery崩溃

时间:2016-05-25 22:37:01

标签: ios swift watch-os-2 health-kit hksamplequery

所以我在分布式应用程序上调试崩溃时遇到问题。我不能亲自重现这次崩溃。但是,似乎有大量用户遇到了问题。我怀疑它与HKSampleQuery有关,可能没有结果。但我不明白为什么当许多用户说他们允许我的应用访问他们的心率数据时,没有结果会返回。然后,即使我们没有结果,它应该只运行方法'displayError()'但它崩溃....

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")
}

终端中符号化的Atos崩溃输出

InterfaceController。(runHKSampleQuery(HKSampleType,mostRecentPredicate:NSPredicate,sortDescriptor:NSSortDescriptor) - &gt;())。(闭包#1)。(闭包#1)(在AppleWatch Extension中)(InterfaceController.swift:76)

我在接口控制器的第76行添加了4 ****,这是应该发生崩溃的地方。

从崩溃中堆叠帧

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)

对此的任何帮助都会很棒。我不明白为什么会发生什么。干杯

1 个答案:

答案 0 :(得分:1)

看看你认为可能是问题的一点:

您可能希望将代码更改为

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

作为一个单独的问题,您似乎没有对error对象做任何事情,这可能有助于您发现真正的问题。