2014-06-13 46 views
0

我試圖保存包含30個圖像的NSDictionary。我調用方法來保存我的ViewController的viewDidDisappear中的字典。問題是UI保存時凍結。這是一個小滯後,不到一秒鐘,但有點煩人。你有任何想法讓它更流暢嗎?也許我應該異步保存字典,也許在一個塊中,但我不知道如何使用它們。保存圖像時的UI凍結

這裏是我的儲蓄等方法獲得:

+ (NSDictionary*)getProgramImages{ 
    NSString *path = [DataManager getProgramImagesFileDirectory]; 
    NSDictionary *programImages = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; 

    return programImages; 
} 

+ (void)saveProgramImages:(NSDictionary*)programImages{ 
    NSString *path = [DataManager getProgramImagesFileDirectory]; 

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:programImages]; 
    [data writeToFile:path options:NSDataWritingAtomic error:nil]; 
} 

非常感謝您的幫助!

鮑里斯

+0

問題是,你正在調用主線程上的保存功能,如果它太重,它會凍結UI。在此處閱讀有關線索的更多信息:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/CreatingThreads/CreatingThreads.html – Milo

回答

1

您可以使用下面的代碼嘗試打包函數調用,該代碼使用Grand Central Dispatch在後臺線程上運行該代碼。目前無法進行測試,看看是否可以解決您的問題。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    // call that function inside here 
}); 
1

也許dispatch_async可以幫助你理順在主線程上運行的代碼。

dispatch_async(dispatch_get_main_queue(),^{ 
     //your code goes here 
    }); 
1

有很多方法可以解決您的問題。您應該閱讀下面的內容 Concurrency Programming

Grand Central Dispatch是一個不錯的選擇。