3

我有一个正在杀死我的 EXC_BAD_ACCESS。我看不出它可能来自哪里。

弧码。SymptomRating 是一个托管对象,当然:

__block DPLSymptomRating *rating;
self.editMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];  // retained: strong

NSManagedObjectContext *moc = self.editMOC; //editSymptom.managedObjectContext;

[moc performBlockAndWait:^{
    NSError __autoreleasing *error = nil;
    moc.parentContext = DPLPersonalHxDataStore.shared.managedObjectContext;

    rating = [NSEntityDescription insertNewObjectForEntityForName:@"SymptomRating" inManagedObjectContext:moc];
    [moc assignObject:rating toPersistentStore:[NSPersistentStore MR_defaultPersistentStore]];

    rating.ratingCode = @101;
    rating.symptomCode = 1;
    rating.displayName = @"debug";
    NSAssert(rating!=nil, @"Cannot edit nil rating");
    NSAssert(rating.managedObjectContext==moc, @"");
    NSLog(@"validates for insert: %@", [rating validateForInsert:&error]?@"true":@"false");
    NSAssert(error==nil,@"");
    NSLog(@"inserted objects: %@", moc.insertedObjects);    // 1 object, from above
    NSLog(@"updated objects: %@", moc.updatedObjects);      // empty
    NSLog(@"deleted objects: %@", moc.deletedObjects);      // empty
    [moc save:&error];   // -->FAIL: EXC_BAD_ACCESS (code=2, address=0x2)
    NSLog(@"can we get here?");  // NOPE
    NSAssert(error==nil, @"");
}];

无论并发类型是 Main 还是 Private,以及使用或不使用 -performBlock: 或 -performBlockAndWait,都会崩溃并出现相同的错误:

我有 NSZombieEnabled(我认为——不知道如何验证它是否真的有效)。

回溯:

frame #0: 0x019c0098 libobjc.A.dylib`objc_msgSend + 12
frame #1: 0x0087e5e0 CoreData`_PFObjectIDFastHash64 + 96
frame #2: 0x01fb48d0 CoreFoundation`__CFDictionaryHashKey + 32
frame #3: 0x01f0a114 CoreFoundation`CFBasicHashFindBucket + 1572
frame #4: 0x01f09ad5 CoreFoundation`CFDictionaryGetValue + 133
frame #5: 0x0088cde8 CoreData`-[NSPersistentStoreCache incrementRefCountForObjectID:] + 40
frame #6: 0x0088cd74 CoreData`-[NSSQLCore managedObjectContextDidRegisterObjectsWithIDs:] + 228
frame #7: 0x009528aa CoreData`-[NSPersistentStoreCoordinator(_NSInternalMethods) _informAffectedStoresOfInterestByChildContextInObjectsWithObjectIDs:withSelector:] + 122
frame #8: 0x0088cc7f CoreData`-[NSPersistentStoreCoordinator(_NSInternalMethods) managedObjectContextDidRegisterObjectsWithIDs:] + 47
frame #9: 0x008bd9e9 CoreData`__-[NSManagedObjectContext(_NestedContextSupport) managedObjectContextDidRegisterObjectsWithIDs:]_block_invoke_1 + 73
frame #10: 0x008bce41 CoreData`internalBlockToNSManagedObjectContextPerform + 17
frame #11: 0x01de5953 libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 61
frame #12: 0x01de5e00 libdispatch.dylib`dispatch_barrier_sync_f + 62
frame #13: 0x008bcde5 CoreData`_perform + 117
frame #14: 0x008bd999 CoreData`-[NSManagedObjectContext(_NestedContextSupport) managedObjectContextDidRegisterObjectsWithIDs:] + 73
frame #15: 0x008bd9e9 CoreData`__-[NSManagedObjectContext(_NestedContextSupport) managedObjectContextDidRegisterObjectsWithIDs:]_block_invoke_1 + 73
frame #16: 0x008bcdc4 CoreData`_perform + 84
frame #17: 0x008bd999 CoreData`-[NSManagedObjectContext(_NestedContextSupport) managedObjectContextDidRegisterObjectsWithIDs:] + 73
frame #18: 0x008ac672 CoreData`-[NSManagedObjectContext(_NSInternalAdditions) _informParentStore:ofInterestInObjects:] + 274
frame #19: 0x008991e8 CoreData`-[NSManagedObjectContext save:] + 536
frame #20: 0x000a06fc MyApp`__53-[DPLNotesViewController editRatingForIndexPath:new:]_block_invoke(.block_descriptor=0xbfffe260) + 1836 at DPLNotesViewController.m:270
frame #21: 0x008bcaf3 CoreData`developerSubmittedBlockToNSManagedObjectContextPerform + 99
frame #22: 0x01de5953 libdispatch.dylib`_dispatch_barrier_sync_f_invoke + 61
frame #23: 0x01de5e00 libdispatch.dylib`dispatch_barrier_sync_f + 62
frame #24: 0x008bca48 CoreData`-[NSManagedObjectContext performBlockAndWait:] + 136
frame #25: 0x0009fe61 MyApp`-[DPLNotesViewController editRatingForIndexPath:new:](self=0x0a367eb0, _cmd=0x0010fb59, indexPath=0x083f9200, new='\x01') + 465 at DPLNotesViewController.m:251
frame #26: 0x0009f786 MyApp`-[DPLNotesViewController tableView:didSelectRowAtIndexPath:](self=0x0a367eb0, _cmd=0x02ba0ff6, tableView=0x08c69a00, indexPath=0x083f9200) + 150 at DPLNotesViewController.m:195
frame #27: 0x00bad71d UIKit`-[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1164
frame #28: 0x00bad952 UIKit`-[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 201
frame #29: 0x0143586d Foundation`__NSFireDelayedPerform + 389
frame #30: 0x01fc6966 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
frame #31: 0x01fc6407 CoreFoundation`__CFRunLoopDoTimer + 551
frame #32: 0x01f297c0 CoreFoundation`__CFRunLoopRun + 1888
frame #33: 0x01f28db4 CoreFoundation`CFRunLoopRunSpecific + 212
frame #34: 0x01f28ccb CoreFoundation`CFRunLoopRunInMode + 123
frame #35: 0x021ac879 GraphicsServices`GSEventRunModal + 207
frame #36: 0x021ac93e GraphicsServices`GSEventRun + 114
frame #37: 0x00b1da9b UIKit`UIApplicationMain + 1175

我还能尝试什么?帮助!

4

3 回答 3

7

这个问题原来是一个 iOS 5 错误,其中父上下文显然无法看到他们孩子的新关系。强制 Core Data 在尝试保存之前获取永久对象 ID 可修复问题,如下所示:

[moc obtainPermanentIDsForObjects:@[rating] error:&error];
[moc save:&error];   // --> WORKS NOW
于 2013-07-22T15:16:28.007 回答
1

我可以看到你正在使用魔法唱片。

在这种情况下,您应该使用他们自己的 MR_save 命令。

无论如何,这不是 iOS 5 唯一的错误,而是 iOS 5/6,我无法在 7 中重现它。

因此,当您提到获得永久 ID 帮助时,要解决它。但不仅如此。

就我而言,我在询问了永久 ID 后正在更改子上下文。但是父上下文尚未保存并导致 iOS 6 中的崩溃。我尝试在更改或访问子项之前保存子项和父项,并且它不再崩溃。检查它的一种方法是确保:objectID.isTemporaryID是否

于 2014-04-23T11:29:15.093 回答
0

有一个类似的错误(也与使用魔法记录有关) - 问题原来是在与创建我的上下文的线程不同的线程上创建临时对象,并尝试在保存后访问相同的临时对象引用。

所以不要做这样的事情:

appContext = [NSManagedObject MR_defaultContext]
temporaryObjects = [thingThatWorksInABackgroundThreadWithContext: appContext]
[appContext saveToPersistentStoreAndWait]
NSLog(@"%@", temporaryObjects[0])

而是这样做:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext * _Nonnull localContext) {

    NSArray * tempObjects = [thingThatWorksInSameThreadWithContext:localContext]

}
completion:^(BOOL success, NSError *error) {

    NSArray *savedObjects = [NSArray arrayWithArray:[MyManagedObjectType MR_findAll]];
    NSLog(@"%@", savedObjects[0]);

}

您还需要平台的特定声明以避免应用程序突然终止,请参阅 MR 的文档并在此处保存:https ://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Saving-Entities.md

于 2016-05-11T15:31:19.660 回答