【发布时间】:2011-01-18 21:31:36
【问题描述】:
我这几天一直在为这个问题苦苦挣扎,但无济于事,我已经失去了理智。 :|
我的应用程序设置为在活动和后台跟踪设备的位置,我正在使用 CoreData 创建和存储跟踪数据。主要的 CoreData 实体是“Route”,它有许多“RoutePoint”实体。当设备从应用的 CLLocationManager 接收到新位置时,新的 RoutePoint 对象将添加到其父 Route。
当用户停止跟踪时,他们可以选择保存或放弃路线。丢弃路由只需调用 [_moc rollback],当然 save 会调用 [_moc save]。我正在使用在 AppDelegate 上引用我的 NSManagedObjectContext 实例的方法,而不是在我的任何 UIViewControllers 中都没有保留或释放的单例对象。
如果用户在跟踪处于活动状态时将应用程序发送到后台(因此 Route 和 RoutePoint 实体实际上并未保存到 _moc),则会出现问题。当应用程序返回到活动状态时,用户停止路由选择丢弃它,这是应用程序崩溃时(称为“回滚”)。但是,如果重复相同的过程但用户保存路由而不是丢弃,则不会崩溃并且保存成功。啊?!
发生这种情况时,我在 gdb 中得到的唯一信息是 EXC_BAD_ACCESS,所以我进行了回溯,结果如下:
#0 0x34a80464 in objc_msgSend ()
#1 0x356ea6d6 in -[_PFManagedObjectReferenceQueue _processReferenceQueue:] ()
#2 0x356ea3d4 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _processReferenceQueue:] ()
#3 0x356ea084 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] ()
#4 0x3571fb14 in -[NSManagedObjectContext processPendingChanges] ()
#5 0x357603ee in -[NSManagedObjectContext rollback] ()
#6 0x00031862 in -[RouteHistoryDetailViewController discardRoute] (self=0x4a0fc30, _cmd=0x100941)
#7 0x0003064c in -[RouteHistoryDetailViewController alertView:clickedButtonAtIndex:] (self=0x4a0fc30, _cmd=0x344b7f5f, alertView=0x390aa0, buttonIndex=0)
#8 0x3445d63e in -[UIAlertView(Private) _buttonClicked:] ()
#9 0x35821fec in -[NSObject(NSObject) performSelector:withObject:withObject:] ()
#10 0x341c84ac in -[UIApplication sendAction:to:from:forEvent:] ()
#11 0x341c844c in -[UIApplication sendAction:toTarget:fromSender:forEvent:] ()
#12 0x341c841e in -[UIControl sendAction:to:forEvent:] ()
#13 0x341c8170 in -[UIControl(Internal) _sendActionsForEvents:withEvent:] ()
#14 0x341c89ce in -[UIControl touchesEnded:withEvent:] ()
#15 0x341be354 in -[UIWindow _sendTouchesForEvent:] ()
#16 0x341bdcce in -[UIWindow sendEvent:] ()
#17 0x341a8fc6 in -[UIApplication sendEvent:] ()
#18 0x341a8906 in _UIApplicationHandleEvent ()
#19 0x320c8f02 in PurpleEventCallback ()
#20 0x3580f6fe in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ ()
#21 0x3580f6c2 in __CFRunLoopDoSource1 ()
#22 0x35801f7c in __CFRunLoopRun ()
#23 0x35801c86 in CFRunLoopRunSpecific ()
#24 0x35801b8e in CFRunLoopRunInMode ()
#25 0x320c84aa in GSEventRunModal ()
#26 0x320c8556 in GSEventRun ()
#27 0x341dc328 in -[UIApplication _run] ()
#28 0x341d9e92 in UIApplicationMain ()
#29 0x00002982 in main (argc=1, argv=0x2fdff5a8)
所以调用 [_moc rollback] 似乎是一个问题,但我已经进行了广泛的搜索并尝试了各种方法,甚至在 UIViewController 中保留和释放我的 MOC 实例,但还没有任何效果。很明显,当应用程序进入后台时出现问题,但它与 MOC 有关,那么保存时不会发生类似的崩溃吗?
非常感谢您的建议! :)
【问题讨论】: