Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I share one UIManagedDocument between different objects?
    text
    copied!<p>I've already taken a look at <a href="https://stackoverflow.com/questions/9204025/how-do-i-create-a-global-uimanageddocument-instance-per-document-on-disk-shared">How do I create a global UIManagedDocument instance per document-on-disk shared by my whole application using blocks?</a> but I don't really get it.</p> <p>What I want to achieve is that there should be only one UIManagedDocument - a core data database - for the whole app. Different Objects should be calling a method and get the one and only UIManagedDocument.</p> <p>I use a helper class with a class method:</p> <pre><code>+ (UIManagedDocument *)getsharedDatabase:(NSString *)databaseName { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:databaseName]; // url is now "&lt;Documents Directory&gt;/&lt;databaseName&gt;" if (![[NSFileManager defaultManager] fileExistsAtPath:[url absoluteString]]) { // does not exist on disk, so create one UIManagedDocument *managedDocument = [[UIManagedDocument alloc] initWithFileURL:url]; return managedDocument; } else { UIManagedDocument *managedDocument = **?????** return managedDocument; } } </code></pre> <p>As you can see by the question marks, I don't know how to get the existing file. I checked the UIManagedDocument class reference but couldn't find it.</p> <p>Can you help me, please? Many thanks in advance.</p> <p><b>EDIT</b> I was wondering ... what about a singleton method such as:</p> <pre><code>+ (UIManagedDocument *) sharedDatabase { NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; url = [url URLByAppendingPathComponent:@"databaseName"]; // url is now "&lt;Documents Directory&gt;/databaseName" static UIManagedDocument *managedDocument = nil; static dispatch_once_t mngddoc; dispatch_once(&amp;mngddoc, ^{ managedDocument = [[UIManagedDocument alloc] initWithFileURL:url]; }); return managedDocument; } </code></pre>
 

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