Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 调用ManagedObjectContext save时执行错误访问(代码=1):&;错误]_Ios_Objective C_Iphone_Core Data - Fatal编程技术网

Ios 调用ManagedObjectContext save时执行错误访问(代码=1):&;错误]

Ios 调用ManagedObjectContext save时执行错误访问(代码=1):&;错误],ios,objective-c,iphone,core-data,Ios,Objective C,Iphone,Core Data,很抱歉,我要问的问题似乎与之前发布的问题相同,但是,到目前为止,我已经看完了所有的问题,但都无济于事 在某些情况下,我会看到EXC_BAD_访问崩溃-非常重复 这个应用程序是一个基本的消息传递应用程序,我正在开发它,以便更好地理解Objective C和iOS编程 我有两个TableView,一个包含与您交谈的人的列表,另一个包含对话的所有消息的视图。如果我在应用程序启动期间将对话初始化为CoreData,我就没有问题 当我从空表视图开始新的对话时,一切似乎都正常。我可以和那个人来回交谈。当我返

很抱歉,我要问的问题似乎与之前发布的问题相同,但是,到目前为止,我已经看完了所有的问题,但都无济于事

在某些情况下,我会看到EXC_BAD_访问崩溃-非常重复

这个应用程序是一个基本的消息传递应用程序,我正在开发它,以便更好地理解Objective C和iOS编程

我有两个TableView,一个包含与您交谈的人的列表,另一个包含对话的所有消息的视图。如果我在应用程序启动期间将对话初始化为CoreData,我就没有问题

当我从空表视图开始新的对话时,一切似乎都正常。我可以和那个人来回交谈。当我返回到主消息列表时,我再次在表视图中看到新线程。当我重新进入线程并尝试向对话中添加更多消息时,就会出现问题-崩溃。当我重新启动应用程序时,我可以毫无问题地继续。这只是第一个引起问题的问题。我觉得这与在子视图中创建的上下文有关,并且在返回父视图时被释放

我使用的是一个单例NSManagedObjectContext,当前已初始化为(AppDelegate.m):

在AppDelegate.m中,我还有一个保存方法:

- (void)saveContext
{
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;

    if (managedObjectContext != nil) {
        [managedObjectContext performBlockAndWait:^{
            NSError *error = nil;
            if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
                NSLog(@"[APPDELEGATE] Unresolved error %@, %@", error, [error userInfo]);
            }
        }];
    }
}
无论我在ViewDidLoad中执行任何fetchedResultsController工作,我都会这样调用一个实用程序类

+ (NSManagedObjectContext*) getContextFromDelegate{
    AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext* context = [app managedObjectContext];

    return context;
}
最后,崩溃的堆栈跟踪:

(lldb) bt
* thread #1: tid = 0x9dba, 0x371a3f66 libobjc.A.dylib`objc_msgSend + 6, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x77656962)
    frame #0: 0x371a3f66 libobjc.A.dylib`objc_msgSend + 6
    frame #1: 0x29724370 CoreData`__77-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]_block_invoke + 1832
    frame #2: 0x296b11a0 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 180
    frame #3: 0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 190
    frame #4: 0x29650832 CoreData`-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 114
    frame #5: 0x298ddf14 CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    frame #6: 0x29838e4c CoreFoundation`_CFXNotificationPost + 1800
    frame #7: 0x2a56dec0 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 76
    frame #8: 0x296507a2 CoreData`-[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 78
    frame #9: 0x2965073a CoreData`-[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 298
    frame #10: 0x2964ecda CoreData`-[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2434
    frame #11: 0x2964d976 CoreData`-[NSManagedObjectContext save:] + 222
  * frame #12: 0x000f4730 wormhole`__26-[AppDelegate saveContext]_block_invoke(.block_descriptor=<unavailable>) + 112 at AppDelegate.m:605
    frame #13: 0x296b11a0 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 180
    frame #14: 0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 190
    frame #15: 0x000f46ac wormhole`-[AppDelegate saveContext](self=0x1555dde0, _cmd=0x00196329) + 180 at AppDelegate.m:603
    frame #16: 0x0010bfae wormhole`+[ClientUtilities saveContext](self=0x001cc580, _cmd=0x00196329) + 162 at ClientUtilities.m:372
    frame #17: 0x000f8966 wormhole`+[CoreDataUtils saveMessage::](self=0x001cc418, _cmd=0x001935ff, new_msg=0x169964e0, personRecord=0x16abc080) + 750 at CoreDataUtils.m:73
    frame #18: 0x000bc2be wormhole`-[MessagesViewController pressSendButton:](self=0x16ae4670, _cmd=0x001930d0, sender=0x16adf1d0) + 950 at MessagesViewController.m:737
    frame #19: 0x2cdfe196 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 70
    frame #20: 0x2cdfe138 UIKit`-[UIControl sendAction:to:forEvent:] + 44
    frame #21: 0x2cde8d1c UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 584
    frame #22: 0x2cdfdb68 UIKit`-[UIControl touchesEnded:withEvent:] + 584
    frame #23: 0x2cdfd842 UIKit`-[UIWindow _sendTouchesForEvent:] + 522
    frame #24: 0x2cdf7150 UIKit`-[UIWindow sendEvent:] + 540
    frame #25: 0x2cdcd8c4 UIKit`-[UIApplication sendEvent:] + 196
    frame #26: 0x2d041372 UIKit`_UIApplicationHandleEventFromQueueEvent + 13874
    frame #27: 0x2cdcc306 UIKit`_UIApplicationHandleEventQueue + 1294
    frame #28: 0x298ebfd6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
    frame #29: 0x298eb3ea CoreFoundation`__CFRunLoopDoSources0 + 222
    frame #30: 0x298e9a68 CoreFoundation`__CFRunLoopRun + 768
    frame #31: 0x29836b30 CoreFoundation`CFRunLoopRunSpecific + 476
    frame #32: 0x29836942 CoreFoundation`CFRunLoopRunInMode + 106
    frame #33: 0x30c16050 GraphicsServices`GSEventRunModal + 136
    frame #34: 0x2ce2c6f0 UIKit`UIApplicationMain + 1440
    frame #35: 0x000f830c wormhole`main(argc=1, argv=0x003c6aec) + 108 at main.m:14
(lldb)bt
*线程#1:tid=0x9dba,0x371a3f66 libobjc.A.dylib`objc_msgSend+6,队列='com.apple.main thread',停止原因=EXC_BAD_访问(代码=1,地址=0x77656962)
帧0:0x371a3f66 libobjc.A.dylib`objc#u msgSend+6
帧#1:0x29724370 CoreData`uu 77-[NSFetchedResultsController(PrivateMethods)managedObjectContextDidChange:][uBlock_invoke+1832
帧#2:0x296b11a0核心数据` developerSubmittedBlockToNSManagedObjectContextPerform+180
帧#3:0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:+190
帧#4:0x29650832 CoreData`-[NSFetchedResultsController(PrivateMethods)\u managedObjectContextDidChange:+114
帧#5:0x298ddf14核心基金会``通知中心`正在向观察者发出呼叫
帧#6:0x29838e4c CoreFoundation`\u CFXNotificationPost+1800
帧#7:0x2a56dec0 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:+76
帧#8:0x296507a2 CoreData`-[NSManagedObjectContext(\u NSInternalNotificationHandling)\u PostObjectsIDChangeNotificationWithUserInfo:+78
帧#9:0x2965073a CoreData`-[NSManagedObjectContext(\u NSInternalChangeProcessing)\u createAndPostChangeNotification:带删除:带更新:带刷新:][298
帧#10:0x2964ecda CoreData`-[NSManagedObjectContext(_NSInternalChangeProcessing)\u processRecentChanges:+2434
帧#11:0x2964d976 CoreData`-[NSManagedObjectContext保存:][222
*帧#12:0x000f4730虫洞`\uuuuu26-[AppDelegate saveContext]\uBlock\uInvoke(.block\u描述符=)+112位于AppDelegate.m:605
帧#13:0x296b11a0核心数据` developerSubmittedBlockToNSManagedObjectContextPerform+180
帧#14:0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:+190
帧#15:0x000f46ac虫洞`-[AppDelegate saveContext](self=0x1555dde0,_cmd=0x00196329)+180位于AppDelegate处。m:603
框架#16:0x0010bfae虫洞`+[ClientUtilities saveContext](self=0x001cc580,_cmd=0x00196329)+162位于ClientUtilities.m:372
帧#17:0x000f8966虫洞`+[CoreDataUtils保存消息::](self=0x001cc418,_cmd=0x001935ff,new_msg=0x169964e0,personRecord=0x16abc080)+750位于CoreDataUtils。m:73
帧#18:0x000bc2be虫洞`-[MessagesViewController按send按钮:](self=0x16ae4670,_cmd=0x001930d0,sender=0x16adf1d0)+950在MessagesViewController.m:737
帧#19:0x2cdfe196 UIKit`-[UIApplication sendAction:to:from:forEvent:+70
帧#20:0x2cdfe138 UIKit`-[UIControl发送操作:到:forEvent:+44
帧#21:0x2cde8d1c UIKit`-[UIControl _发送操作预防:withEvent:][584
帧#22:0x2cdfdb68 UIKit`-[UIControl触摸预定:withEvent:][584
帧#23:0x2cdfd842 UIKit`-[UIWindow\u sendTouchesForEvent:+522
帧#24:0x2cdf7150 UIKit`-[UIWindow sendEvent:+540
帧#25:0x2cdcd8c4 UIKit`-[UIApplication sendEvent:][196
帧#26:0x2d041372 UIKit`\u UIApplicationHandleEventFromQueueEvent+13874
帧#27:0x2cdcc306 UIKit`\u UIApplicationHandleEventQueue+1294
帧#28:0x298ebfd6 CoreFoundation`\uuu CFRUNLOOP\u正在调用\u OUT\u以执行\u源0\u函数\uuu+14
帧#29:0x298eb3ea CoreFoundation`u CFRunLoopDoSources0+222
帧#30:0x298e9a68 CoreFoundation`u CFRunLoopRun+768
帧#31:0x29836b30 CoreFoundation`CFRunLoopRunSpecific+476
帧#32:0x29836942 CoreFoundation`CFRunLoopRunInMode+106
帧#33:0x30c16050 GraphicsServices`GSEventRunModal+136
帧#34:0x2ce2c6f0 UIKit`UIApplicationMain+1440
帧#35:0x000f830c虫洞`main(argc=1,argv=0x003c6aec)+108位于main。m:14
最后一个被调用的日志行是崩溃前父视图控制器中的“didChangeObject:Update”


我被难住了。

看起来虫洞是问题的根源……什么是[self-persistentStoreCoordinator]呢?您是否有可能在没有协调器的情况下获得managedObjectContext?另外,您是否在主线程上调用saveContext助手方法?
(lldb) bt
* thread #1: tid = 0x9dba, 0x371a3f66 libobjc.A.dylib`objc_msgSend + 6, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x77656962)
    frame #0: 0x371a3f66 libobjc.A.dylib`objc_msgSend + 6
    frame #1: 0x29724370 CoreData`__77-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]_block_invoke + 1832
    frame #2: 0x296b11a0 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 180
    frame #3: 0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 190
    frame #4: 0x29650832 CoreData`-[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 114
    frame #5: 0x298ddf14 CoreFoundation`__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    frame #6: 0x29838e4c CoreFoundation`_CFXNotificationPost + 1800
    frame #7: 0x2a56dec0 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 76
    frame #8: 0x296507a2 CoreData`-[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 78
    frame #9: 0x2965073a CoreData`-[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 298
    frame #10: 0x2964ecda CoreData`-[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2434
    frame #11: 0x2964d976 CoreData`-[NSManagedObjectContext save:] + 222
  * frame #12: 0x000f4730 wormhole`__26-[AppDelegate saveContext]_block_invoke(.block_descriptor=<unavailable>) + 112 at AppDelegate.m:605
    frame #13: 0x296b11a0 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 180
    frame #14: 0x296b1342 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 190
    frame #15: 0x000f46ac wormhole`-[AppDelegate saveContext](self=0x1555dde0, _cmd=0x00196329) + 180 at AppDelegate.m:603
    frame #16: 0x0010bfae wormhole`+[ClientUtilities saveContext](self=0x001cc580, _cmd=0x00196329) + 162 at ClientUtilities.m:372
    frame #17: 0x000f8966 wormhole`+[CoreDataUtils saveMessage::](self=0x001cc418, _cmd=0x001935ff, new_msg=0x169964e0, personRecord=0x16abc080) + 750 at CoreDataUtils.m:73
    frame #18: 0x000bc2be wormhole`-[MessagesViewController pressSendButton:](self=0x16ae4670, _cmd=0x001930d0, sender=0x16adf1d0) + 950 at MessagesViewController.m:737
    frame #19: 0x2cdfe196 UIKit`-[UIApplication sendAction:to:from:forEvent:] + 70
    frame #20: 0x2cdfe138 UIKit`-[UIControl sendAction:to:forEvent:] + 44
    frame #21: 0x2cde8d1c UIKit`-[UIControl _sendActionsForEvents:withEvent:] + 584
    frame #22: 0x2cdfdb68 UIKit`-[UIControl touchesEnded:withEvent:] + 584
    frame #23: 0x2cdfd842 UIKit`-[UIWindow _sendTouchesForEvent:] + 522
    frame #24: 0x2cdf7150 UIKit`-[UIWindow sendEvent:] + 540
    frame #25: 0x2cdcd8c4 UIKit`-[UIApplication sendEvent:] + 196
    frame #26: 0x2d041372 UIKit`_UIApplicationHandleEventFromQueueEvent + 13874
    frame #27: 0x2cdcc306 UIKit`_UIApplicationHandleEventQueue + 1294
    frame #28: 0x298ebfd6 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14
    frame #29: 0x298eb3ea CoreFoundation`__CFRunLoopDoSources0 + 222
    frame #30: 0x298e9a68 CoreFoundation`__CFRunLoopRun + 768
    frame #31: 0x29836b30 CoreFoundation`CFRunLoopRunSpecific + 476
    frame #32: 0x29836942 CoreFoundation`CFRunLoopRunInMode + 106
    frame #33: 0x30c16050 GraphicsServices`GSEventRunModal + 136
    frame #34: 0x2ce2c6f0 UIKit`UIApplicationMain + 1440
    frame #35: 0x000f830c wormhole`main(argc=1, argv=0x003c6aec) + 108 at main.m:14