MagicalRecord saveToPersistentStoreWithCompletion似乎不止一次发生导致大量滞后/ ui阻塞

时间:2014-01-15 15:37:18

标签: objective-c nsmanagedobjectcontext magicalrecord

我尝试使用MagicalRecord一次导入大量实体,但saveToPersistentStoreWithCompletion在控制台内打印多次,需要很长时间才能完成。

这是保存区!

[[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
        NSLog(@"Saved the database changes");
}];

我很欣赏有大量要保存的项目,但MagicalRecord保存多个相同信息的上下文是否正常?如果是这样,我可以在后台直接保存一个上下文吗?

提前感谢您的帮助。

-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x18df55f0) → Saving <NSManagedObjectContext (0x18df55f0): *** UNNAMED ***> on *** BACKGROUND THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x18df55f0) → Save Parents? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x18df55f0) → Save Synchronously? 0
-[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x18df55f0) Context UNNAMED is about to save. Obtaining permanent IDs for new 18677 inserted objects
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765c3f0) → Saving <NSManagedObjectContext (0x1765c3f0): *** DEFAULT ***> on *** BACKGROUND THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765c3f0) → Save Parents? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765c3f0) → Save Synchronously? 0
-[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x1765c3f0) Context DEFAULT is about to save. Obtaining permanent IDs for new 18677 inserted objects
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765de80) → Saving <NSManagedObjectContext (0x1765de80): *** BACKGROUND SAVING (ROOT) ***> on *** MAIN THREAD ***
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765de80) → Save Parents? 1
-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:](0x1765de80) → Save Synchronously? 0
-[NSManagedObjectContext(MagicalRecord) MR_contextWillSave:](0x1765de80) Context BACKGROUND SAVING (ROOT) is about to save. Obtaining permanent IDs for new 18677 inserted objects
__70-[NSManagedObjectContext(MagicalSaves) MR_saveWithOptions:completion:]_block_invoke25(0x1765de80) → Finished saving: <NSManagedObjectContext (0x1765de80): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***

更新1:

我刚刚发现使用以下速度可以将时间加快一点但是它似乎仍然可以节省3倍。

[MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext){
} completion:^(BOOL success, NSError *error){
    NSLog(@"Saved the database changes");
}];

1 个答案:

答案 0 :(得分:1)

由于使用指定方法执行的任何操作正在执行mainThread上的任务,因此您会看到UI-Blocking&amp;滞后。

如果您返回文件MagicalRecord+Actions.h&amp;验证,然后您将找到适用于backgroundThread的其他方法,即:

+ (void) saveWithBlock:(void(^)(NSManagedObjectContext *localContext))block;

+ (void) saveWithBlock:(void(^)(NSManagedObjectContext *localContext))block completion:(MRSaveCompletionHandler)completion;

尝试使用此&amp;它将避免mainThread阻塞,因为操作将在后台线程中执行。

希望有所帮助。