【发布时间】:2015-03-20 07:10:25
【问题描述】:
我尝试使用block 和有时(0.5% 率)加载所有日历事件我得到NSInvalidArgumentException 导致崩溃:
var allCals = _eventStore.calendarsForEntityType(EKEntityTypeEvent)
var predicate:NSPredicate! = _eventStore.predicateForEventsWithStartDate(yearsAgo, endDate:toAgo, calendars:allCals)
_eventStore.enumerateEventsMatchingPredicate(predicate, usingBlock:{
(event:EKEvent!, stop:UnsafeMutablePointer<ObjCBool>) in
if (event.title != nil
&& event.calendar != nil
&& event.calendar.calendarIdentifier != nil
&& event.lastModifiedDate != nil
){
if event.attendees != nil{ // < -- SOMETIMES LEADS TO CRASH!!
//...
}
else{ // standalone
//...
}
}
//......
})// end block
完整的堆栈跟踪:[线程崩溃 + 异常]
Thread : Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x2917649f __exceptionPreprocess + 126
1 libobjc.A.dylib 0x36970c8b objc_exception_throw + 38
2 CoreFoundation 0x29096291 -[__NSSetM addObject:] + 608
3 EventKit 0x29b4eacf -[EKPersistentObject primitiveRelationValueForKey:] + 502
4 Foundation 0x29dc1617 -[NSObject(NSKeyValueCoding) valueForKey:] + 202
5 EventKit 0x29b5afc5 -[EKObjectToManyRelation _loadedItems] + 120
6 EventKit 0x29b5aef3 -[EKObjectToManyRelation _effectiveItems] + 102
7 EventKit 0x29b5ae69 -[EKObjectToManyRelation items] + 20
8 EventKit 0x29b5d179 -[EKCalendarItem attendees] + 36
9 MyApp 0x000d8c78 _TTSf2n_n_n_n_n_d_i_n_n___TFFC8MyApp17WmBuildGroupsTask14doInBackgroundFS0_FT_T_U_FTGSqCSo7EKEvent_GVSs20UnsafeMutablePointerV10ObjectiveC8ObjCBool__T_ (WmBuildGroupsTask.swift:421)
10 MyApp 0x000d3f3c _TPA__TTSf2n_n_n_n_n_d_i_n_n___TFFC8MyApp17WmBuildGroupsTask14doInBackgroundFS0_FT_T_U_FTGSqCSo7EKEvent_GVSs20UnsafeMutablePointerV10ObjectiveC8ObjCBool__T_ (WmBuildGroupsTask.swift)
11 MyApp 0x000d96b8 reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.EKEvent>, @unowned Swift.UnsafeMutablePointer<ObjectiveC.ObjCBool>) -> (@unowned ()) to @callee_unowned @objc_block (@unowned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.EKEvent>, @unowned Swift.UnsafeMutablePointer<ObjectiveC.ObjCBool>) -> (@unowned ()) (WmBuildGroupsTask.swift)
12 EventKit 0x29ba72a7 __60-[EKEventStore enumerateEventsMatchingPredicate:usingBlock:]_block_invoke + 130
13 EventKit 0x29bad6af __41-[EKPredicateSearch startWithCompletion:]_block_invoke_2 + 690
14 libdispatch.dylib 0x36ed07bb _dispatch_call_block_and_release + 10
15 libdispatch.dylib 0x36ed9dab _dispatch_root_queue_drain + 866
16 libdispatch.dylib 0x36edacd7 _dispatch_worker_thread3 + 94
17 libsystem_pthread.dylib 0x37031e31 _pthread_wqthread + 668
18 libsystem_pthread.dylib 0x37031b84 start_wqthread + 8
Thread : Crashed: com.apple.root.default-qos
0 libsystem_kernel.dylib 0x36fb6dfc __pthread_kill + 8
1 libsystem_pthread.dylib 0x37034d37 pthread_kill + 62
2 libsystem_c.dylib 0x36f56909 abort + 76
3 libc++abi.dylib 0x362919c9 __cxa_bad_cast
4 libc++abi.dylib 0x362ab671 default_unexpected_handler()
5 libobjc.A.dylib 0x36970f25 _objc_terminate() + 192
6 libc++abi.dylib 0x362a8de3 std::__terminate(void (*)()) + 78
7 libc++abi.dylib 0x362a85a9 __cxxabiv1::exception_cleanup_func(_Unwind_Reason_Code, _Unwind_Exception*)
8 libobjc.A.dylib 0x36970d5f objc_exception_throw + 250
9 CoreFoundation 0x29096291 -[__NSSetM addObject:] + 608
10 EventKit 0x29b4eacf -[EKPersistentObject primitiveRelationValueForKey:] + 502
11 Foundation 0x29dc1617 -[NSObject(NSKeyValueCoding) valueForKey:] + 202
12 EventKit 0x29b5afc5 -[EKObjectToManyRelation _loadedItems] + 120
13 EventKit 0x29b5aef3 -[EKObjectToManyRelation _effectiveItems] + 102
14 EventKit 0x29b5ae69 -[EKObjectToManyRelation items] + 20
15 EventKit 0x29b5d179 -[EKCalendarItem attendees] + 36
16 MyApp 0x000d8c78 _TTSf2n_n_n_n_n_d_i_n_n___TFFC8MyApp17WmBuildGroupsTask14doInBackgroundFS0_FT_T_U_FTGSqCSo7EKEvent_GVSs20UnsafeMutablePointerV10ObjectiveC8ObjCBool__T_ (WmBuildGroupsTask.swift:421)
17 MyApp 0x000d3f3c _TPA__TTSf2n_n_n_n_n_d_i_n_n___TFFC8MyApp17WmBuildGroupsTask14doInBackgroundFS0_FT_T_U_FTGSqCSo7EKEvent_GVSs20UnsafeMutablePointerV10ObjectiveC8ObjCBool__T_ (WmBuildGroupsTask.swift)
18 MyApp 0x000d96b8 reabstraction thunk helper from @callee_owned (@owned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.EKEvent>, @unowned Swift.UnsafeMutablePointer<ObjectiveC.ObjCBool>) -> (@unowned ()) to @callee_unowned @objc_block (@unowned Swift.ImplicitlyUnwrappedOptional<ObjectiveC.EKEvent>, @unowned Swift.UnsafeMutablePointer<ObjectiveC.ObjCBool>) -> (@unowned ()) (WmBuildGroupsTask.swift)
19 EventKit 0x29ba72a7 __60-[EKEventStore enumerateEventsMatchingPredicate:usingBlock:]_block_invoke + 130
20 EventKit 0x29bad6af __41-[EKPredicateSearch startWithCompletion:]_block_invoke_2 + 690
21 libdispatch.dylib 0x36ed07bb _dispatch_call_block_and_release + 10
22 libdispatch.dylib 0x36ed9dab _dispatch_root_queue_drain + 866
23 libdispatch.dylib 0x36edacd7 _dispatch_worker_thread3 + 94
24 libsystem_pthread.dylib 0x37031e31 _pthread_wqthread + 668
线9 a.e. WmBuildGroupsTask.swift:421 指向这一行 if event.attendees != nil{。
任何人都可以传播如何摆脱这种崩溃或至少使代码安全免受崩溃的启发吗?
[编辑 1]
我也试过了:
if event.hasAttendees {
if let attArray:NSArray = event.attendees{ // crashes here
/* ... */
}
}
有趣的是,从命令行:po event.hasAttendees 返回 true
当po event.attendees 返回null。但还是崩溃
[编辑 2]
升级到 Swift 1.2 后会出现同样的崩溃(在 Crashlytics 中)。也试过了:
if event.hasAttendees {
if let attArray:[EKParticipant] = event.attendees as? [EKParticipant] // crash here
{
/* ... */
}
}
有问题的活动是指 Google 之类的日历,正如我指出的 99.5% 以上,相同的活动正常运行,并且我成功吸引了与会者。
我不是 iOS Guru,但看起来像是它的内部 iOS 问题:
libobjc.A.dylib 0x36970d5f objc_exception_throw + 250
9 CoreFoundation 0x29096291 -[__NSSetM addObject:] + 608
10 EventKit 0x29b4eacf -[EKPersistentObject primitiveRelationValueForKey:] + 502
11 Foundation 0x29dc1617 -[NSObject(NSKeyValueCoding) valueForKey:] + 202
12 EventKit 0x29b5afc5 -[EKObjectToManyRelation _loadedItems] + 120
13 EventKit 0x29b5aef3 -[EKObjectToManyRelation _effectiveItems] + 102
14 EventKit 0x29b5ae69 -[EKObjectToManyRelation items] + 20
15 EventKit 0x29b5d179 -[EKCalendarItem attendees] + 36
谢谢,
【问题讨论】:
-
你能试试
if event.hasAttendees && event.attendees != nil {吗? -
@rintaro 很好,我会检查的。如果我停止关注异常,我会通知您发布评论作为答案。谢谢
-
在 Swidt 2.0 中将实现 try/catch