Doug Smith
Doug Smith

Reputation: 29326

Why does my bool value in Core Data change when I reload the app?

When I swipe my UITableViewCell (whose objects are from Core Data) it sets the object for the cell as "read" (in code: isRead becomes YES).

This is accomplished as such:

- (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];
}

However, the very next time the app loads the article is back in the unread state (or isRead is equal to NO). I made isRead a transient property in Core Data so whenever it's accessed I can do things to it, and I manipulate it as such:

- (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;
}

Is that somehow confusing it? I don't see what would be causing the change.

Upvotes: 0

Views: 87

Answers (1)

Kevin
Kevin

Reputation: 17586

Quite simply, transient properties are not stored persistently; they are never written to the database which is why they default back to 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."

Upvotes: 6

Related Questions