【问题标题】:Using most recent saved data with Core Data将最近保存的数据与 Core Data 一起使用
【发布时间】:2013-10-28 12:38:23
【问题描述】:

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

- (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 立即为我提供正确的计数?

【问题讨论】:

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


    【解决方案1】:

    它不起作用,因为[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];
            }];
        }
    }];
    

    【讨论】:

    • 已解决。谢谢!甚至不必放置“weakSelf”......我直接使用“Self”并且它起作用了。那么有什么理由我应该改用“weakSelf”吗?
    • @Armand:你是对的。 self 没有保留任何块,因此这里不需要 __weak 修饰符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多