Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I properly use MagicalRecord for importing data from a webservice without blocking the UI
    text
    copied!<p>I am using Magical Record in my application to parse server responses to generate / update / delete my datamodel objects and also use it to make alle the fetches i need in my app.</p> <p>For the importing I have my own abstraction class that has two methods. Also I could replace MagicalRecord for the importing very easy that way.</p> <pre><code>- (void)importInBackgroundUsingBlock:(void (^)(NSManagedObjectContext *localContext))block completion:(void (^)(BOOL success))completion </code></pre> <p>and</p> <pre><code>- (void)saveInBackgroundUsingBlock:(void (^)(NSManagedObjectContext *localContext))block completion:(void (^)(BOOL success))completion </code></pre> <p>The importInBackground is used for all the data that i get via the API. In my case those calls are all made after a AFNetworking request. The saveInBackground is used whenever the user interacts with the app so that something need to change in the DB. This second method is so that I can prioritize stuff that the user caused (as he wants immediate feedback).</p> <p>In my abstraction class i also have two serial queues (one for each method) so that I have two different background threads.</p> <p>Those methods now look like that...</p> <pre><code>- (void)saveInBackgroundUsingBlock:(void (^)(NSManagedObjectContext *localContext))block completion:(void (^)())completion { dispatch_async(self.backgroundQueue, ^{ [MagicalRecord saveUsingCurrentThreadContextWithBlockAndWait:block]; dispatch_async(dispatch_get_main_queue(), ^{ if (completion) { completion(); } }); }); } </code></pre> <p>The problem now is that if i have a look at the app in Instruments, I see that a lot of the time (for the save) is spent on the main thread. Also the saveInBackground often has to wait for import to finish, which I don't really understand...</p> <p>It seems as if </p> <pre><code>- (void)MR_saveWithOptions:(MRSaveContextOptions)mask completion:(MRSaveCompletionHandler)completion; </code></pre> <p>called on the main thread for saving the parent of the context. This seems to be the main context. But why can it be that saving the original context where changes happend is fast, but saving the main context is taking so long?!? I understand that changes have to be merged... but it's taking much much longer than the original save?!?</p> <p>Can I have a separate context for saving to disc? Would that help in my case?</p> <p>Would be great if you guys could help me improve my code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload