IOS Core Data asynchronous saving
<h1>Problem Statement</h1>

<p>I am having trouble saving server side data <strong>asynchronously</strong>.</p>

<h1>Structure</h1>

<p>I am using the following structure of NSManagedObjectContext in order of parent to child:</p>

<ol>
<li>writerManagedObjectContext (NSPrivateQueueConcurrencyType)</li>
<li>masterManagedObjectContext (NSMainQueueConcurrencyType)</li>
<li>backgroundManagedObjectContext (NSPrivateQueueConcurrencyType)</li>
</ol>

<h1>Code</h1>

<p>I am using the following code to save data</p>

<pre><code>[backgroundManagedObjectContext performBlock:^{
    [backgroundManagedObjectContext save:nil];
    [masterManagedObjectContext performBlock:^{ // Starts blocking UI from here
        [masterManagedObjectContext save:nil];
        [writerManagedObjectContext performBlock:^{
            [writerManagedObjectContext save:nil];
        }]
    }]
}]
</code></pre>

<h1>Problem</h1>

<p>The code saves fine. The <code>backgroundManagedObjectContext</code> also saves asynchronously. However, both <code>masterManagedObjectContext</code> and <code>writerManagedObjectContext</code> refuse to be saved asynchronously and blocks the UI thread. (I know it blocks the UI thread because I tried to perform actions which have nothing to do with Core Data and they were also blocked. This is not a case of the persistent coordinator not being accessible)</p>

<h1>Questions</h1>

<ol>
<li>What would be the reason the above code blocks the main thread?</li>
<li>Am I correct in assuming I can call the above code from anywhere since the <code>save</code> will be called in each respective thread/context?</li>
</ol>

<p>Any help would be much appreciated.</p>

<p><strong>Edit</strong></p>

<p><a href="http://floriankugler.com/2013/04/29/concurrent-core-data-stack-performance-shootout/" rel="nofollow">http://floriankugler.com/2013/04/29/concurrent-core-data-stack-performance-shootout/</a></p>

<p>Apparently the freeze comes from the attempt to propagate to the parent NSManagedObjectContext. The article seems to elude to the fact that it is impossible to have a truly asynchronous save to the main context.</p>

<p>The data was heavily linked, 5MB, and took approximately 40s to save to psc. I don't think I'll use a parallel structure as described in the post since the code base is already large as it is. I would appreciate any strategies I can utilize to decrease this freeze.</p>