【问题标题】:How can I find the SQLite file using documentDirectory of Core Data?如何使用 Core Data 的 documentDirectory 找到 SQLite 文件?
【发布时间】:2014-09-20 13:04:14
【问题描述】:

我遇到一个问题,每次我尝试使用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

【问题讨论】:

  • Model.sqlite。该文件有一个扩展名。

标签: ios sqlite uitableview core-data nsdocumentdirectory


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2018-07-02
    相关资源
    最近更新 更多