删除Core Data中的单行无效

时间:2013-08-14 04:01:31

标签: ios objective-c cocoa-touch core-data

我正在尝试创建一个日记应用程序,我正在努力删除Core Data中的条目。当我尝试删除对象时没有任何反应,没有错误消息,没有任何内容,并且该条目仍然存在且未删除。代码:

- (void)deleteButtonPressed:(UIButton *) button {
    NSLog(@"Button Pressed");
    NSLog(@"%i", button.tag);

    UIView *viewToRemove = [self.view viewWithTag:button.tag];
    [viewToRemove removeFromSuperview];

    UIView *secondViewToRemove = [self.view viewWithTag:button.tag];
    [secondViewToRemove removeFromSuperview];

    UIView *thirdViewToRemove = [self.view viewWithTag:button.tag];
    [thirdViewToRemove removeFromSuperview];

    UIView *fourthViewToRemove = [self.view viewWithTag:button.tag];
    [fourthViewToRemove removeFromSuperview];

    JournalrAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Entrys" inManagedObjectContext:context];

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:entityDesc];

    NSManagedObject *matches = nil;
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];

    matches = objects[1];
    [context deleteObject:matches];

    if (![context save:&error]) {
        NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
        return;
    }
}

1 个答案:

答案 0 :(得分:0)

也许是因为这句话:

matches = objects[1];

数组是零索引的。那么也许你正在寻找第一个元素(objects[0])? 做一些NSLog以查看是否确实要删除。

此外,如果您希望在视图中进行某种视觉更新,请不要这样做。您必须手动更新视图以反映数据库中的更改。