Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?
    primarykey
    data
    text
    <p>I am trying to design a helper method which will retrieve a UIManagedDocument, then open and return it, so that I may access the same UIManagedDocument from several places in my app.</p> <p>I am having trouble with the asynchronous nature of this as I am not too familiar with blocks. Ideally the sequence of events would be this:</p> <ol> <li>Class X calls the helper method to retrieve the UIManagedDocument and includes a block of code to run when the opened document is returned.</li> <li>The class method retrieves the UIManagedDocument and calls openWithCompletionHandler or saveToURL as necessary, and includes a block of code to run when the opened document is returned.</li> <li>openwithCompletionHandler or saveToURL complete their task, and return with success = YES and run the code in its block</li> <li>The class method completes its task and returns with an open UIManagedDocument and runs the code in its block</li> </ol> <p>Can I pass the original block through somehow?</p> <p>Here's my code so far. Any thoughts hugely appreciated, thanks.</p> <pre><code>// This is a dictionary where the keys are "Vacations" and the objects are URLs to UIManagedDocuments static NSMutableDictionary *managedDocumentDictionary = nil; // This typedef has been defined in .h file: // typedef void (^completion_block_t)(UIManagedDocument *vacation); // The idea is that this class method will run the block when its UIManagedObject has opened @implementation MyVacationsHelper + (void)openVacation:(NSString *)vacationName usingBlock:(completion_block_t)completionBlock { // Try to retrieve the relevant UIManagedDocument from managedDocumentDictionary UIManagedDocument *doc = [managedDocumentDictionary objectForKey:vacationName]; // Get URL for this vacation -&gt; "&lt;Documents Directory&gt;/&lt;vacationName&gt;" NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:vacationName]; // If UIManagedObject was not retrieved, create it if (!doc) { // Create UIManagedDocument with this URL doc = [[UIManagedDocument alloc] initWithFileURL:url]; // Add to managedDocumentDictionary [managedDocumentDictionary setObject:doc forKey:vacationName]; } // If document exists on disk... if ([[NSFileManager defaultManager] fileExistsAtPath:[url path]]) { [doc openWithCompletionHandler:^(BOOL success) { // Can I call the completionBlock from above in here? // How do I pass back the opened UIDocument }]; } else { [doc saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) { // As per comments above }]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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