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

Dou*_*ith 0 core-data objective-c ios

当我刷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];
}
Run Code Online (Sandbox Code Playgroud)

但是,下次应用程序加载文章时,将返回未读状态(或isRead等于NO).我isRead在Core Data中创建了一个瞬态属性,所以无论什么时候访问它我都可以做它,我操纵它:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

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

Kev*_*vin 6

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

"瞬态属性是您定义为模型一部分的属性,但不会作为实体实例数据的一部分保存到持久性存储中."