【发布时间】:2013-11-02 21:38:20
【问题描述】:
我正在使用 Magical Record 2.1 来处理我的应用程序中的数据持久性。如果我创建一个新实体,设置一些它的属性并保存,它工作正常。但是,稍后,如果我获取该实体,更新它的属性并保存,后续获取将拥有新数据,直到我终止应用程序并重新启动。在新应用会话期间,旧数据会重新出现。
这就是我创建新实体的方式:
self.localContext = [NSManagedObjectContext MR_defaultContext];
self.theNewListing = [Listing MR_createInContext:self.localContext];
我正在使用 MRDefaultContext 阅读了这篇博文:http://saulmora.com/2013/09/15/why-contextforcurrentthread-doesn-t-work-in-magicalrecord/
在这种情况下,我的主要属性是一个字典,我这样设置它:
NSMutableDictionary *tempDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"A description", @"slFieldDescription", etc, etc, nil];
self.theNewListing.dataDictionary = tempDictionary;
我是这样保存的:
[self.presentingViewController dismissViewControllerAnimated:YES completion:^(void) {
[self.localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error){
if(!success) {
NSLog(@"%@", error);
}
else {
[self.thePresentingVC refreshCollectionViews:nil];
}
}];
}];
我在集合视图中显示我的数据,此时一切看起来都很好。如果我终止并重新启动,数据仍然存在。
如果我再次获取实体并像这样更新属性:
NSMutableDictionary *newTempDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"A new description", @"slFieldDescription", etc, etc, nil];
self.theNewListing.dataDictionary = newTempDictionary;
然后使用与上面相同的保存代码进行保存,然后使用下面的代码更新我的收藏视图,一切看起来都不错。
self.listingsArray = [[NSMutableArray alloc] initWithArray:[Listing MR_findAllSortedBy:@"dateListed" ascending:NO]];
[self.mainCollectionView reloadData];
也就是说,直到我退出应用并重新启动。
如果你想知道,我正在使用 FTASync,它只支持 MR 2.1,这就是我没有升级到最新版本的原因。
谢谢!
【问题讨论】:
标签: ios core-data magicalrecord magicalrecord-2.1