Objective c 关键问题的xcode核心数据编辑值

Objective c 关键问题的xcode核心数据编辑值,objective-c,xcode,core-data,Objective C,Xcode,Core Data,这里有个问题 我的BOOL被编辑,并且在最后一次NSLog中获得成功,但是当我关闭ViewController然后再次进入(更新表)时,BOOL返回到第一个值。也就是说,我的[context save:&error]中出现了错误;功能。 有什么想法吗 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { for (int i=0; i<[self tabl

这里有个问题

我的BOOL被编辑,并且在最后一次NSLog中获得成功,但是当我关闭ViewController然后再次进入(更新表)时,BOOL返回到第一个值。也就是说,我的[context save:&error]中出现了错误;功能。 有什么想法吗

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

for (int i=0; i<[self tableView:tableView numberOfRowsInSection:0]; i++) {

    AccountCell *cell = (AccountCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
    [cell setSelected:(i==indexPath.row) animated:NO];
    NSManagedObject *user = [arr objectAtIndex:indexPath.row];
    [user setValue:[NSNumber numberWithBool:(i==indexPath.row)] forKey:@"active"];
    NSLog(@"Index: %i, Active State: %@", i,[user valueForKey:@"active"]);
    NSError *error;

    if (![context save:&error]) {
        NSLog(@"Saving changes to context failed: %@", error);
    } else {
        // The changes have been persisted.
        NSLog(@"Saved data success");
    }
}
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(nsindepath*)indepath{
对于(int i=0;i一些建议:

将save语句置于for循环之外会更有意义

你需要检查一下

  • 您的托管对象上下文有效(非nil)
  • 数组中对象的上下文与保存的上下文相同
  • “active”属性(包括拼写)在您的模型和托管对象中已正确配置(为了更清晰起见,您可能希望子类化,而不是依赖于KVC)
  • error变量中有一些内容
我还认为还有其他一些设计缺陷。例如,您正在获取单元格并设置它们的选定状态,即使它们可能不可见。在我看来,您应该在cellforrowatinexpath中根据底层托管对象的状态执行此操作


至于取消选择同一分区中的所有其他用户,您是对的,循环可能是不可避免的。但我认为,一次获取分区中的所有用户,然后循环它们以设置所需的“活动”属性会更有效。

为什么要调用
[上下文保存:&错误]
两次?你想完成什么,因为这可能会帮助我们给你一个更好的答案。好问题…我现在就编辑它。我尝试编辑键“active”的值,我会这样做,但只是暂时-它不会保存它。你确定每次都有相同的托管对象上下文吗?