如何使用Core Data的documentDirectory找到SQLite文件?

时间:2014-07-29 07:52:33

标签: ios sqlite uitableview core-data nsdocumentdirectory

我遇到了一个问题,每次我尝试使用documentDirectory获取SQLite文件时,我都找不到它。我真的很想知道SQLite文件放在项目文件中的位置以及如何获取它的名称以便我可以找到SQLite文件。

- (void) save
{
    // Create UIManagedDocument
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSURL *documentDirectory = [[fileManager URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask]firstObject];
    NSString *documentName = @"Model";
    NSURL *url = [documentDirectory URLByAppendingPathComponent:documentName];
    UIManagedDocument *document = [[UIManagedDocument alloc]initWithFileURL:url];
    if ([fileManager fileExistsAtPath:[url path]]) {
        [document openWithCompletionHandler:^(BOOL success) {
            if (success) {
                if (document.documentState == UIDocumentStateNormal) {
                    // Get a ManagedObjectContext
                    NSManagedObjectContext *context = document.managedObjectContext;

                    // Set managed object (entity)
                    NSManagedObject *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

                    // Set value for the attribute (which are "name" and "age") of the entity
                    [aPerson setValue:self.nameTextField.text forKey:@"name"];
                    [aPerson setValue:self.ageTextField.text forKey:@"age"];

                    // Check whether there is an error
                    NSError *error = nil;
                    if (![context save:&error]) {
                        NSLog(@"Can't save due to %@%@", error, [error localizedDescription]);
                    }

                    // Close the window
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }
            if (!success) {
                NSLog(@"couldn't open document at %@", url);
            }
        }];
    }
    else {
        [document saveToURL:url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                if (document.documentState == UIDocumentStateNormal) {
                    // Get a ManagedObjectContext
                    NSManagedObjectContext *context = document.managedObjectContext;

                    // Set managed object (entity)
                    NSManagedObject *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];

                    // Set value for the attribute (which are "name" and "age") of the entity
                    [aPerson setValue:self.nameTextField.text forKey:@"name"];
                    [aPerson setValue:self.ageTextField.text forKey:@"age"];

                    // Check whether there is an error
                    NSError *error = nil;
                    if (![context save:&error]) {
                        NSLog(@"Can't save due to %@%@", error, [error localizedDescription]);
                    }

                    // Close the window
                    [self dismissViewControllerAnimated:YES completion:nil];
                }
            }
            if (!success) {
                NSLog(@"couldn't open document at %@", url);
            }
        }];
    }
}

每次运行应用程序时,调试器都会说:

2014-07-29 15:41:22.476 TableAndCoreData[2502:60b] couldn't open document at file:///Users/Mike/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/DB1F0215-2BF2-43B5-ADF2-76ABC9E2CD16/Library/Documentation/Model

1 个答案:

答案 0 :(得分:0)

试试这个:  NSURL * documentDirectory = [[fileManager URLsForDirectory: NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];