数组索引超出界限问题(NSRangeException) - [NSManagedObjectContext save:]

时间:2015-04-22 09:54:31

标签: ios xcode swift appdelegate sigabrt

我的iOS应用程序因NSRangeException上的-[NSManagedObjectContext save:]而崩溃。在任何地方找不到任何其他帮助。我该如何解决这个问题?我没有得到任何内存地址或任何我可以使用的内容......

2015-04-22 14:16:38.078 heavenhelp[33559:1734247] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5]'
*** First throw call stack:
(
    0   CoreFoundation                      0x0167f746 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x00f40a97 objc_exception_throw + 44
    2   CoreFoundation                      0x01553b73 -[__NSArrayM objectAtIndex:] + 243
    3   CoreData                            0x00859cf3 -[NSSQLCore recordToManyChangesForObject:inRow:usingTimestamp:inserted:] + 2531
    4   CoreData                            0x00856a0b -[NSSQLCore _populateRow:fromObject:timestamp:inserted:] + 2923
    5   CoreData                            0x00776e24 -[NSSQLCore prepareForSave:] + 1764
    6   CoreData                            0x00775e3d -[NSSQLCore saveChanges:] + 461
    7   CoreData                            0x0073f15e -[NSSQLCore executeRequest:withContext:error:] + 638
    8   CoreData                            0x0083ee75 __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 5349
    9   CoreData                            0x008492ff gutsOfBlockToNSPersistentStoreCoordinatorPerform + 191
    10  libdispatch.dylib                   0x035f4bef _dispatch_client_callout + 14
    11  libdispatch.dylib                   0x035d7b0d _dispatch_barrier_sync_f_invoke + 144
    12  libdispatch.dylib                   0x035d723f dispatch_barrier_sync_f + 105
    13  CoreData                            0x008383f7 _perform + 183
    14  CoreData                            0x0073ec8b -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 459
    15  CoreData                            0x0076ee09 -[NSManagedObjectContext save:] + 1529
    16  heavenhelp                          0x000b6834 _TF10heavenhelp11saveContextFT_T_ + 324
    17  heavenhelp                          0x0015368d _TFC10heavenhelp26ConversationViewController17viewWillDisappearfS0_FSbT_ + 701
    18  heavenhelp                          0x001536ef _TToFC10heavenhelp26ConversationViewController17viewWillDisappearfS0_FSbT_ + 63
    19  UIKit                               0x020a4292 -[UIViewController _setViewAppearState:isAnimating:] + 706
    20  UIKit                               0x020a4904 -[UIViewController __viewWillDisappear:] + 106
    21  UIKit                               0x020bcd1d -[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 200
    22  UIKit                               0x020cafec -[UINavigationController _startCustomTransition:] + 1028
    23  UIKit                               0x020d8e00 -[UINavigationController _startDeferredTransitionIfNeeded:] + 712
    24  UIKit                               0x020d9a51 -[UINavigationController __viewWillLayoutSubviews] + 57
    25  UIKit                               0x02253750 -[UILayoutContainerView layoutSubviews] + 213
    26  UIKit                               0x01fce57a -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 668
    27  libobjc.A.dylib                     0x00f56771 -[NSObject performSelector:withObject:] + 70
    28  QuartzCore                          0x01d5ee47 -[CALayer layoutSublayers] + 144
    29  QuartzCore                          0x01d52925 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 403
    30  QuartzCore                          0x01d5277a _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    31  QuartzCore                          0x01caec52 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 284
    32  QuartzCore                          0x01cb00e5 _ZN2CA11Transaction6commitEv + 487
    33  QuartzCore                          0x01cb07fc _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    34  CoreFoundation                      0x015a086e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    35  CoreFoundation                      0x015a07b0 __CFRunLoopDoObservers + 400
    36  CoreFoundation                      0x015961ea __CFRunLoopRun + 1226
    37  CoreFoundation                      0x01595a5b CFRunLoopRunSpecific + 443
    38  CoreFoundation                      0x0159588b CFRunLoopRunInMode + 123
    39  GraphicsServices                    0x046cc2c9 GSEventRunModal + 192
    40  GraphicsServices                    0x046cc106 GSEventRun + 104
    41  UIKit                               0x01f3b106 UIApplicationMain + 1526
    42  heavenhelp                          0x000a5c94 main + 180
    43  libdyld.dylib                       0x0361fac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我用save:线程安全的代码:

static var onceToken: dispatch_once_t = 0
static var singleton: CoreDataHelper!
static var sharedInstance: CoreDataHelper {
    get {
        dispatch_once(&onceToken, {
            self.singleton = CoreDataHelper()
        })
        return singleton
    }
}

我的xcdatamodeld(相关部分):

xcdatamodeld

编辑:我已编辑代码以反映我的更改,以使我的NSManagedObjectContext线程安全。我现在所做的一切,都是在我CoreDataHelper的一个实例上进行的,我在上面初始化了。我发现我可以通过进入一个对话,添加消息,进入另一个对话并在那里添加消息来触发崩溃。我添加了xcdatamodeld

3 个答案:

答案 0 :(得分:2)

我显然将对话和消息之间的一对多关系设置为“有序”。这不正确......我已经将NSManagedObject的类更改为使用NSMutableSet而不是NSMutableOrderedSet。

显然,这会在save方法中导致NSRangeException ...

答案 1 :(得分:0)

AppDelegate中的某个地方,您正在创建NSArray

通过添加Exception Breakpoint,您可以检查导致错误的行。

添加异常断点的方法在以下链接上。

http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html

答案 2 :(得分:0)

你说你已经保存了保存方法线程。你能否展示一些代码?

通常我正在做这样的事情:

// create object with concurrency where you need it
[[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];


// wrapper for saving
- (BOOL)saveContext
{
    __block BOOL success = NO;

    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        [managedObjectContext performBlockAndWait:^{
            if([managedObjectContext hasChanges])
            {
                NSError *error = nil;
                success = [managedObjectContext save:&error];

                if (success == NO)
                {
                    NSLog(@"Unresolved error %@", error.localizedDescription);
                }
            }
        }];
    }

    return success;
}