我第一次嘗試MagicalRecord 3.0,我無法使它工作。MagicalRecord 3.0不保存。沒有對象的第二個版本
這就是我正在做的。
1設置堆棧:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[MagicalRecord setupClassicStackWithSQLiteStoreNamed:@"Model"];}
我的模型名稱爲 「Model.xcdatamodeld」。
2 - 創建實體並保存:
Preferences *p = [Preferences createEntity];
p.visibilityWindow = @"08:00-23:30";
[[NSManagedObjectContext MR_context] saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
if (success) {
NSLog(@"You successfully saved your context.");
} else if (error) {
NSLog(@"Error saving context: %@", error.description);
}
}];
3-加載數據:
NSArray *matches;
NSError * error = nil;
matches = [Preferences MR_findAll];
Preferences *p = nil;
NSLog(@"Fetch request data %@",matches);
if(!matches || error || ([matches count] >1)){
//handle error;
NSLog(@"Error %@ matches count %lu", error, (unsigned long)[matches count]);
}else if ([matches count]){
p = [matches firstObject];
NSLog(@"Preferences found in coredata %@", p);
}else{
NSLog(@"No matches %i", [matches count]);
}
我也 「清理」 的文件表明:
- (void)applicationWillTerminate:(UIApplication *)application {
[MagicalRecord cleanUp];
}
這是工作t當我使用傳統的核心數據框架時非常好。
它說我已經成功保存了。但是,當我退出應用程序,並嘗試再次運行它不再工作。 我在做什麼錯?
此外,從我閱讀的帖子,大家都談論「MR_defaultContext」。它是否被棄用?
似乎沒有工作。不從創建實體返回。甚至沒有達到保存方法。將嘗試保存在主隊列上下文中。 – jonypz 2014-11-24 22:48:31
我還應該指出'MR_findAll'也使用不同的主隊列上下文,所以如果您仍在使用MR_context,則需要調用MR_findAllInContext:NSManagedObjectContext MR_context]。 – 2014-11-24 22:48:49
我想在3.0中想使用'[[MagicalRecordStack defaultStack]上下文]'。這似乎等同於舊的「MR_defaultContext」。 – 2014-11-24 22:55:13