سؤال

I am using RestKit for networking my Core Data model from a JSON api feed, which works well for when I GET and POST data, however I am having an issue when saving the value of a local attribute which does not need to be networked with the feed.

I have a list of notes displayed on a collectionView table, (read from the JSON feed, mapped to Core Data through RestKit). I need to distinguish when a note has been read or not, so I've added a Boolean attribute to the core data model which records if the note has been read, allowing the note's text to change from Bold font to normal, for read / unread notes respectively.

This is the code I have used to set the value of the attribute and then save to Core Data,

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

PBNote *note = [self.fetchedResultsController objectAtIndexPath:indexPath];

NSManagedObjectContext *context = self.managedObjectContext;

[note setRead:[NSNumber numberWithBool:YES]];

NSError *error = nil;
if (![context save:&error]) {
    NSLog(@"Unable to save context with error %@", error);
} else {
    NSLog(@"saved setRead to YES");
}

}

When the collectionViewCell is created, this Boolean value determines wether the note is written in Bold font or not.

Every time I run this, it appears to save, and the note font changes accordingly, though when I re-launch app, it has not persisted.

IF ,however, I select the cell, setting the attribute value and POST a note etc within the app, THIS DOES save the attribute value and is there on re-launch of the app? This has puzzled me, and is why I'm wondering if RestKit means I should be using a different method to save the data.

If anyone can help, this would be greatly appreciated.

Thanks in advance

هل كانت مفيدة؟

المحلول

When you save the context, the save doesn't pass the changes up to the persistent store. Instead of using save: on the context, use saveToPersistentStore: instead to both save and push the changes up so that they are persisted to disk.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top