2014-11-24 30 views
0

我第一次嘗試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」。它是否被棄用?

enter image description here

回答

0

一對夫婦的想法: 看代碼爲MagicalRecord 3.0,MR_context返回NSPrivateQueueConcurrencyType。使用MR_createEntity似乎使用不同的主隊列上下文。嘗試使用MR_createEntityInContext:創建實體並傳入MR_context

此外,不知道何時調用這三種方法,請嘗試使用MR_saveToPersistentStoreAndWaitWithError:

編輯: 可能做測試的最快的是要改變第2步:

Preferences *p = [Preferences createEntity]; 
p.visibilityWindow = @"08:00-23:30"; 

NSError *error; // add this 
BOOL success = [p.managedObjectContext MR_saveToPersistentStoreWithError:error] // change this 
if (success) { 
    NSLog(@"You successfully saved your context."); 
} else if (error) { 
    NSLog(@"Error saving context: %@", error.description); 
} 
+0

似乎沒有工作。不從創建實體返回。甚至沒有達到保存方法。將嘗試保存在主隊列上下文中。 – jonypz 2014-11-24 22:48:31

+0

我還應該指出'MR_findAll'也使用不同的主隊列上下文,所以如果您仍在使用MR_context,則需要調用MR_findAllInContext:NSManagedObjectContext MR_context]。 – 2014-11-24 22:48:49

+2

我想在3.0中想使用'[[MagicalRecordStack defaultStack]上下文]'。這似乎等同於舊的「MR_defaultContext」。 – 2014-11-24 22:55:13

0

我有同樣的問題...還有就是我用(與你的東西),我也加入選擇存儲與UIBackgroundTaskIdentifier(opcional)

-

Preferences *p = [Preferences createEntity]; 
p.visibilityWindow = @"08:00-23:30"; 

[SomethingManagerObject saveIntoContext] 

-

SomethingManagerObject

+ (void) saveWithContext{ 

// THIS IS opcional!!! -> Prepar for BackgroundTask 
UIApplication * application = [UIApplication sharedApplication]; 

__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{ 
    [application endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 
}]; 

// Get the context 
NSManagedObjectContext *localContext = [[MagicalRecordStack defaultStack] context]; 

// Save the modification in the context 
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) { 

    [application endBackgroundTask:bgTask]; 
    bgTask = UIBackgroundTaskInvalid; 

}]; 
} 

或者你也可以與實體的managedObjectContext保存。在saveWithContext方法只需添加參數併發送p.managedObjectContext。內部更改[[MagicalRecordStack defaultStack] context]

SomethingManagerObject是一個NSObject,您可以使用這種類的方法... save ... create ...

相關問題