1

I'm using Magical Record 2.1 to handle the data persistence in my app. If I create a new entity, set some it's attributes and save, it works fine. However, later, if I fetch that entity, update it's attributes and save, subsequent fetches have the new data until I terminate the app and restart. During the new app session the old data reappears.

This is how I create a new entity:

self.localContext = [NSManagedObjectContext MR_defaultContext];
self.theNewListing = [Listing MR_createInContext:self.localContext];

I'm using MRDefaultContext having read this blog post: http://saulmora.com/2013/09/15/why-contextforcurrentthread-doesn-t-work-in-magicalrecord/

In this case my main attribute is a dictionary, and I set it like this:

NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"A description", @"slFieldDescription", etc, etc, nil];
self.theNewListing.dataDictionary = tempDictionary;

This is how I save it:

[self.presentingViewController dismissViewControllerAnimated:YES completion:^(void) {

    [self.localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error){
        if(!success) {
            NSLog(@"%@", error);
        }
        else {
            [self.thePresentingVC refreshCollectionViews:nil];
        }
    }];
}];

I display my data in a collection view, and at this point everything looks fine. If I terminate and restart the data is still there.

If I fetch the entity again and update the attributes like this:

NSMutableDictionary *newTempDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"A new description", @"slFieldDescription", etc, etc, nil];
self.theNewListing.dataDictionary = newTempDictionary;

Then save using the same save code as above, and then update my collection view using the code below, all looks good.

self.listingsArray = [[NSMutableArray alloc] initWithArray:[Listing MR_findAllSortedBy:@"dateListed" ascending:NO]];
[self.mainCollectionView reloadData];

That is, until I quit the app and restart.

If you're wondering, I'm using FTASync, and this only supports MR 2.1, which is why I haven't upgraded to the latest version.

Thanks!

4

2 回答 2

0

不确定 MR,也不知道为什么需要它。如果该框架可以为您提供主要上下文,只需调用本机核心数据保存。

[context save:nil];
于 2013-11-02T22:09:03.297 回答
0

破解它!

我注意到我的其他属性正在保存,而不是这个(这个包含 UI 中显示的所有数据),这导致我进行了另一条调查。

因此,我似乎需要使用不可变字典来存储这些数据,如下所述:

核心数据保存问题:无法更新可转换属性(NSArray)

于 2013-11-08T10:04:16.277 回答