当我重新加载应用程序时,为什么Core Data中的bool值会发生变化?

时间:2013-11-04 22:35:36

标签: ios objective-c core-data

当我滑动我的UITableViewCell(其对象来自Core Data)时,它将单元格的对象设置为“read”(在代码中:isRead变为YES)。

这是这样完成的:

- (void)swipedToMarkCellRead:(Article *)article {
     if ([article.isRead isEqualToNumber:@YES]) {
          article.isRead = @NO;
     }
     else {
          article.isRead = @YES;
     }

     NSManagedObjectContext *context = self.managedObjectContext;
     NSError *error;
     [context save:&error];
}

但是,下次应用加载文章时,会回到未读状态(或isRead等于NO)。我在核心数据中创建了isRead一个临时属性,所以无论何时访问它我都能做到这一点,并且我操纵它:

- (NSNumber *)isRead {
    [self willAccessValueForKey:@"isRead"];
    NSNumber *isRead = [self primitiveValueForKey:@"isRead"];
    [self didAccessValueForKey:@"isRead"];

    // If at 100% progress (finished) or it's already marked as read
    if ([self.progress intValue] >= 1 || [isRead boolValue]) {
        isRead = @YES;
    }
    else {
        isRead = @NO;
    }

    return isRead;
}

这有点令人困惑吗?我不知道会导致这种变化的原因。

1 个答案:

答案 0 :(得分:6)

很简单,瞬态属性不会持久存储;它们永远不会写入数据库,这就是它们默认返回NO的原因。

"Transient properties are properties that you define as part of the model, but which are not saved to the persistent store as part of an entity instance's data."