0

我正在尝试制作日记应用程序,并且正在从 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;
    }
}
4

1 回答 1

0

也许是因为这条线:

matches = objects[1];

数组是零索引的。那么,也许您正在寻找第一个元素 ( objects[0])?做一些 NSLogs 看看是否真的有东西要删除。

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

于 2013-08-18T11:00:17.647 回答