首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >watchOS 2 HealthKit HKSampleQuery崩溃

watchOS 2 HealthKit HKSampleQuery崩溃
EN

Stack Overflow用户
提问于 2016-05-25 22:37:01
回答 1查看 244关注 0票数 0

因此,我在一个分布式应用程序上调试崩溃时遇到了问题。我无法亲自再现这场车祸。然而,似乎有大量的用户有一个问题。我怀疑这是用HKSampleQuery做的,可能是没有结果的返回。但是我不明白为什么当很多用户说他们允许我的应用程序访问他们的心率数据时,结果就不会回来了。然后,即使没有结果,它也应该运行'displayError()‘方法,但是它会崩溃.

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

代码语言:javascript
复制
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) -> ().(闭包#1) (闭包#1)( AppleWatch扩展) (InterfaceController.swift:76)

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

堆栈框架来自崩溃

代码语言:javascript
复制
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)

在这方面任何帮助都会很好。我不明白为什么会发生这种事。干杯

EN

回答 1

Stack Overflow用户

发布于 2016-05-25 23:08:17

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

您可能希望将代码更改为

代码语言:javascript
复制
if let results = results {
    self.handleHKResults(results)
}else{
    //handle error
}

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37448778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档