从核心数据模型中删除实体时断言失败

时间:2015-04-27 10:58:23

标签: ios xcode uitableview core-data azure-mobile-services

我正在使用Microsoft Azure开发应用程序。我正面临这个问题,但无法理解背后的原因。当用户点击“注销”按钮时,我想清除所有本地数据存储(如果可用)。目前我在核心数据模型中有7个实体,我必须在代码下面清除:

- (void) deleteAllObjects: (NSString *) entityDescription  {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = delegate.managedObjectContext;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;
    NSArray *items = [context executeFetchRequest:fetchRequest error:&error];
    for (NSManagedObject *managedObject in items) {
        [context deleteObject:managedObject];
        NSLog(@"%@ object deleted",entityDescription);
    }
    if (![context save:&error]) {
        NSLog(@"Error deleting %@ - error:%@",entityDescription,error);
    }
}

我以下面的方式调用这个方法:

NSArray *entityNames = [[NSArray alloc] initWithObjects:@"Medicine", @"Allergy",@"MedicalInfo", @"Note", @"Profile", @"TodoItem", @"Vital", nil];
for (NSString *entity in entityNames) {
        [self deleteAllObjects:entity];
}

我的错误

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-3318.16.14/UITableView.m:1582  2015-04-27 16:21:00.334 ARKAA-IOS[18570:4080663] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'  *** First throw call stack:
(0x21a8fc1f 0x2f23ac8b 0x21a8faf5 0x2275fd7f 0x251197ef 0x8b98b 0x65a9db 0x65a9c7 0x65e3ed 0x21a559d1 0x21a540d1 0x219a2211 0x219a2023 0x28d9b0a9 0x24fae1d1 0x142501 0x2f7baaaf) libc++abi.dylib: terminating with uncaught exception of type NSException

0 个答案:

没有答案