2010-09-14 135 views
0

我有一個核心數據對象「實體」有2個字段 - 「名稱」(一個NSString *)和「類型」(一個EntityType)。 EntityType是使用1個字段名稱(NSString *)的另一個核心數據。
我創建並使用在功能上與下面的代碼相似的代碼刪除一個實體對象 -iphone核心數據刪除導致崩潰

Entity *e1 = [NSEntityDescription insertNewObjectForEntityName:"@Entity" inManagedObjectContext: context] 

EntityType *type = [NSEntityDescription insertNewObjectForEntityName:"@EntityType" inManagedObjectContext: context] 

e1.type = type; 

現在我刪除的實體對象無需給予任何值或保存到持久性存儲。

if([e1.name length] == 0) { 

    [context deleteObject:e1]; 

    NSError *nil; 
    if(![context:save &error] { 
    // log error 
    } 
} 

在這一點上,我看到一個崩潰,當我嘗試刪除並保存對象。 (由於未捕獲的異常 'NSRangeException' 原因終止:[NSCFArray removeObjectAtIndex]:索引(0)超過界限(0)]

任何指針/建議讚賞

這裏是被請求的堆棧跟蹤 -

#0 0x3266bdf4 in objc_exception_throw() 
#1 0x32d73b32 in +[NSException raise:format:arguments:]() 
#2 0x32d73ad2 in +[NSException raise:format:]() 
#3 0x33f4a710 in _NSArrayRaiseBoundException() 
#4 0x33f7ba3c in -[NSCFArray removeObjectAtIndex:]() 
#5 0x30537dc6 in -[NSFetchedResultsController(PrivateMethods) _removeObjectInFetchedObjectsAtIndex:]() 
#6 0x3053b50a in -[NSFetchedResultsController(PrivateMethods) _postprocessDeletedObjects:]() 
#7 0x3053aa9a in -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:]() 
#8 0x33f765d8 in _nsnote_callback() 
#9 0x32d9e510 in _CFXNotificationPostNotification() 
#10 0x33f741b2 in -[NSNotificationCenter postNotificationName:object:userInfo:]() 
#11 0x304b4388 in -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:]() 
#12 0x3050768a in -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:]() 
#13 0x3049c2b0 in -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:]() 
#14 0x304cb186 in -[NSManagedObjectContext save:]() 
#15 0x00006d5a in -[MyAppViewController goToMain] (self=0x11b730, _cmd=0x174b3) at /Users/Me/Projects/MyApp/Classes/MyAppViewController.m:611 



2010-09-15 17:37:10.053 MyApp[1273:207] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray removeObjectAtIndex:]: index (0) beyond bounds (0)' 
2010-09-15 17:37:10.066 MApp[1273:207] Stack: (
    853417245, 
    845594132, 
    852966195, 
    852966099, 
    ............ 
    ............. 
+0

請添加崩潰堆棧跟蹤 – 2010-09-15 06:48:50

+0

添加堆棧跟蹤 – 2010-09-15 12:22:39

回答

0

錯誤從fetchedResults陣列的FRC的未來我懷疑這是你不執行FRC委託方法正確,這樣當實現代碼如下嘗試更新,它會嘗試訪問零指數造成一個空陣列。

將FRC連接到任何上下文時,FRC將在您對上下文進行任何更改時自動進行更新,如刪除對象(這是跟蹤堆棧中的通知正在做什麼,告訴FRC進行更新。 )