Jorn
Jorn

Reputation: 1044

Get path to file in Core Data

Is it possible to get the path to files stored inside Core Data, or can I only extract the file itself? I need the path to files stored as NSData attribute in Core Data, not the database itself. Some iOS features often ask for an NSURL rather than the file itself, and I have yet to figure out how to combine this with Core Data.

Upvotes: 0

Views: 1397

Answers (2)

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

There are no "files" inside of the database if you are using SQLite. The database is a single file.

If you are storing binary data in your database then there is no direct way to get to the file under it (that is even assuming you are using external storage as an option).

If you want direct file access to something inside of the SQLite database under Core Data then you are going to need to write it out yourself to another file.

Upvotes: 2

trevorj
trevorj

Reputation: 2039

This is how you can get the relevant NSURL:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory
                                                     inDomains:NSUserDomainMask] firstObject];
NSString *documentName = @"YOUR_DATABASE_NAME";
NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];

To see it in a grander context, this is typically how you'd set up your UIManagedDocument to work with Core Data:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory
                                                 inDomains:NSUserDomainMask] firstObject];
NSString *documentName = @"YOUR_DATABASE_NAME";
NSURL *url = [documentsDirectory URLByAppendingPathComponent:documentName];
self.document = [[UIManagedDocument alloc] initWithFileURL:url];

BOOL fileExists = [fileManager fileExistsAtPath:[url path]];
if (fileExists) {
    [self.document openWithCompletionHandler:^(BOOL success) {
        if (success) {
            self.context = self.document.managedObjectContext;
            // Post notification so others can gather the document and context
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseReady"
                                                                object:self];
        }
        if (!success) NSLog(@"couldn't open file at %@", url);
    }];
} else {
    [self.document saveToURL:url
            forSaveOperation:UIDocumentSaveForCreating
           completionHandler:^(BOOL success) {
               if (success) {
                   self.context = self.document.managedObjectContext;
                   // Post notification so others can gather the document and context
                   [[NSNotificationCenter defaultCenter] postNotificationName:@"DatabaseReady"
                                                                       object:self];
               }
               if (!success) NSLog(@"couldn't open file at %@", url);
           }];
}

Upvotes: 0

Related Questions