将最近保存的数据与Core Data一起使用

时间:2013-10-28 12:38:24

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

我正在核对清单上填写一些项目。我使用以下方法计算已完成的项目:

- (NSUInteger)completedCount {
return [DIDTask MR_countOfEntitiesWithPredicate:[NSPredicate predicateWithFormat:@"completed == YES && list == %@", self]];
}

我遇到的问题是当我在列表上调用方法时 - list.completedCount - 在保存数据后立即它不会给我正确的计数而是值 - 1.仅在应用程序更改后例如屏幕或显示弹出窗口(如下所示),然后list.completedCount给我正确的值。但这对我来说太晚了。

[UIAlertView showAlertViewWithTitle:@"Are you OK?" message:task.name cancelButtonTitle:@"Stop" otherButtonTitles:@[@"Yes", @"No"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
        if (buttonIndex > 0) {
            [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
                [[task MR_inContext:localContext] setCompletedValue:buttonIndex == 1];
            } completion:^(BOOL success, NSError *error) {
            }];
            [self continueAutomaticModeWithList:list taskIndex:index + 1];
        }
    }];

我的问题是如何在保存数据时立即更新或刷新应用程序,以便list.completedCount立即为我提供正确的计数?

1 个答案:

答案 0 :(得分:2)

它不起作用,因为在保存完成之前执行了[self continueAutomaticModeWithList:list taskIndex:index + 1];。你必须将它移动到完成块:

[UIAlertView showAlertViewWithTitle:@"Are you OK?" message:task.name cancelButtonTitle:@"Stop" otherButtonTitles:@[@"Yes", @"No"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
    if (buttonIndex > 0) {
        [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
            [[task MR_inContext:localContext] setCompletedValue:buttonIndex == 1];
        } completion:^(BOOL success, NSError *error) {
            [self continueAutomaticModeWithList:list taskIndex:index + 1];
        }];
    }
}];
相关问题