Get path to file in Core Data

为君一笑 提交于 2019-12-25 06:54:33

问题


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.


回答1:


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.




回答2:


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);
           }];
}


来源:https://stackoverflow.com/questions/25801480/get-path-to-file-in-core-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!