2013-10-28 51 views
0

我正在完成检查清单上的一些项目。我使用下面的方法计算已完成的项目:使用核心数据最新保存的数据

- (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给了我正确的计数,对吗?

回答

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]; 
     }]; 
    } 
}]; 
+0

已解决。谢谢!甚至没有必要把“weakSelf”放在一边...我直接和“Self”一起去了,它工作。那么为什么我应该使用“weakSelf”来代替它? – Armand

+0

@Armand:你说得对。没有一个块被'self'保留,所以'__weak'修饰符在这里不需要。 –