调用ManagedObjectContext时EXC_BAD_ACCESS(code = 1)保存:& error]

时间:2015-07-07 02:10:08

标签: ios objective-c iphone core-data

很抱歉,问一下之前发布的问题是什么,但是,到目前为止,我已经完成了所有问题,但没有用。

在某些情况下,我看到EXC_BAD_ACCESS崩溃 - 非常可重复。

该应用程序是一个基本的消息传递应用程序,我正致力于更好地理解Objective C和iOS编程。

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

当我从空表视图开始新对话时,一切似乎都没问题。我可以来回与人交谈。当我退回主消息列表时,我再次在表视图中看到新线程。当我重新进入线程并尝试向对话添加更多消息时出现问题 - CRASH。当我重新启动应用程序时,我可以继续没有任何问题。它只是第一个导致问题的因素。我觉得它与在子视图中创建的上下文有关,并在我返回到父视图时被释放。

我使用当前初始化的单例NSManagedObjectContext(AppDelegate.m):

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext != nil) {
        return _managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];

    if (coordinator != nil) {
        NSLog(@"Managed Object Context Doesn't exist yet - creating it!");
        _managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
        [_managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return _managedObjectContext;
}

并且在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

被调用的最后一个日志行是&#34; didChangeObject:Update&#34;在崩溃前的父视图控制器中。

我很难过。

0 个答案:

没有答案