我工作的一個core data
應用多線程操作,其中我需要處理很多文件。
我進行插入,刪除,更新等,在使用這些文件的詳細信息的核心數據。
由於處理DATAS的數量巨大,我使用單獨NSManagedObjectContext
爲每個線程操作。(保持爲mainThread主上下文)。
爲了方便NSManagedObjectContext
管理我設定的範圍內成線字典。
我沒有任何使用這種方法的關鍵內存鬆動,但是當我運行xcode儀器泄漏工具時,它顯示與NSManagedObjectContext
相關的泄漏對象的數量。
但是我發現,背景完全消除時,線程退出。(因爲當前線程的操作後,殺死自己,線程詞典還清洗)。
下面給出的代碼用於獲取managedObjectContext
-(NSManagedObjectContext*)managedObjectContext {
NSManagedObjectContext *context = nil;
if ([NSThread isMainThread]) {
if (!managedObjectContext) {
context = [NSManagedObjectContext newContextForPersistentStoreCoordinator:[self persistentStoreCoordinator]];
[self setManagedObjectContext:context];
}
context = managedObjectContext;
}
else {
//find context for this thread.
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
context = [threadDictionary objectForKey:kManagedObjectContextKey];
if (!context) {
//create a new context for this thread.
context = [NSManagedObjectContext newContextForPersistentStoreCoordinator:[self persistentStoreCoordinator]];
[threadDictionary setObject:context forKey:kManagedObjectContextKey];
[context setUndoManager:nil];
//to start observing context through DidSaveNotification.
[self startObserveContext:context];
}
}
return context;
}
而且沒有顯示出泄漏的儀器顯示,當我只使用單/主NSManagedObjectContext.But它,因爲內存的問題,同時處理大量的文件,所以我需要單獨的NSManagedObjectContext爲每個線程操作
任何一個可以告訴我,這將是對這個問題的一個好的解決方案?... 處理多個線程的核心數據方面的任何其他方法,將不勝感激..
謝謝
非常感謝你對你的想法@Marcelo,但問題是,即時通訊已經在我的項目完成了幾乎所有的模塊,我認爲這似乎很難,如果我改變人對這些塊..?......或者你可以發佈你使用的方法? – Raj 2012-02-09 14:21:16
我檢查了你給出的鏈接,他們引用了git hub MagicalRecord中的一個開源項目(http:// github。com/magicalpanda/magicalrecord),同樣的事情(在線程字典中設置上下文),我們也可以在那裏看到 – Raj 2012-02-10 05:13:17