Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a couple of ways to this.</p> <p>Since you are using <code>UIManagedDocument</code> you could take advantage of <code>NSPrivateQueueConcurrencyType</code> for initialize a new <code>NSManagedObjectContext</code> and use <code>performBlock</code> to do your stuff. For example:</p> <pre><code>// create a context with a private queue so access happens on a separate thread. NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; // insert this context into the current context hierarchy context.parentContext = parentContext; // execute the block on the queue of the context context.performBlock:^{ // do your stuff (e.g. a long import operation) // save the context here // with parent/child contexts saving a context push the changes out of the current context NSError* error = nil; [context save:&amp;error]; }]; </code></pre> <p>When you save from the context, data of the private context are pushed to the current context. The saving is only visible in memory, so you need to access the main context (the one linked to the <code>UIDocument</code>) and do a save there (take a look at <a href="https://stackoverflow.com/questions/8424130/does-a-core-data-parent-managedobjectcontext-need-to-share-a-concurrency-type-wi">does-a-core-data-parent-managedobjectcontext-need-to-share-a-concurrency-type-wi</a>).</p> <p>The other way (my favourite one) is to create a <code>NSOperation</code> subclass and do stuff there. For example, declare a <code>NSOperation</code> subclass like the following:</p> <pre><code>//.h @interface MyOperation : NSOperation - (id)initWithDocument:(UIManagedDocument*)document; @end //.m @interface MyOperation() @property (nonatomic, weak) UIManagedDocument *document; @end - (id)initWithDocument:(UIManagedDocument*)doc; { if (!(self = [super init])) return nil; [self setDocument:doc]; return self; } - (void)main { NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; [moc setParentContext:[[self document] managedObjectContext]]; // do the long stuff here... NSError *error = nil; [moc save:&amp;error]; NSManagedObjectContext *mainMOC = [[self document] managedObjectContext]; [mainMOC performBlock:^{ NSError *error = nil; [mainMOC save:&amp;error]; }]; // maybe you want to notify the main thread you have finished to import data, if you post a notification remember to deal with it in the main thread... } </code></pre> <p>Now in the main thread you can provide that operation to a queue like the following:</p> <pre><code>MyOperation *op = [[MyOperation alloc] initWithDocument:[self document]]; [[self someQueue] addOperation:op]; </code></pre> <p>P.S. You cannot start an async operation in the <code>main</code> method of a <code>NSOperation</code>. When the <code>main</code> finishes, delegates linked with that operations will not be called. To say the the truth you can but this involves to deal with run loop or concurrent behaviour.</p> <p>Hope that helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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