2012-02-09 54 views
1

議決核心數據操作在多線程,內存泄漏?

我工作的一個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爲每個線程操作

任何一個可以告訴我,這將是對這個問題的一個好的解決方案?... 處理多個線程的核心數據方面的任何其他方法,將不勝感激..

謝謝

回答

0

我改變了我的代碼如下修改

-(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]]; 
      [context setUndoManager:nil]; 

      //to start observing context through DidSaveNotification. 
      [self startObserveContext:context]; 
      [threadDictionary setObject:context forKey:kManagedObjectContextKey]; 
      [context release] 
      context = nil; 

return [threadDictionary objectForKey:kManagedObjectContextKey]; 
     } 
    } 

    return context; 
} 

Previoulsy的[NSManagedObjectContext newContextForPersistentStoreCoordinator:[self persistentStoreCoordinator]];方法返回自動釋放的NSManagedObjectContext,我去掉了自動釋放,

,並在操作開始{}方法,我添加..

我的NSOperation類

start { 
......... 
........ 
....... 
NSManagedObjectContext *currentContext = (NSManagedObjectContext *)[[[NSThread currentThread] threadDictionary] objectForKey:kManagedObjectContextKey]; 
    [currentContext reset]; 
    [[[NSThread currentThread] threadDictionary] removeObjectForKey:kManagedObjectContextKey]; 
    currentContext = nil; 

} 

所有功能在開始(開始的結束後添加上下文刪除線)。

0

相反線程和線程字典,爲什麼不使用塊?有一個很好的解釋在這裏:http://www.cimgf.com/2011/05/04/core-data-and-threads-without-the-headache/

(我正要張貼我使用的方法,但該教程是更好)

+0

非常感謝你對你的想法@Marcelo,但問題是,即時通訊已經在我的項目完成了幾乎所有的模塊,我認爲這似乎很難,如果我改變人對這些塊..?......或者你可以發佈你使用的方法? – Raj 2012-02-09 14:21:16

+0

我檢查了你給出的鏈接,他們引用了git hub MagicalRecord中的一個開源項目(http:// github。com/magicalpanda/magicalrecord),同樣的事情(在線程字典中設置上下文),我們也可以在那裏看到 – Raj 2012-02-10 05:13:17