0

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

Apple Watch 扩展接口控制器中运行的代码

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) -> ()).(closure #1).(closure #1) (在 AppleWatch 扩展中)(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)

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

4

1 回答 1

1

查看您认为可能是问题的点:

您可能希望将代码更改为

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

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

于 2016-05-25T23:08:17.843 回答