簡體   English   中英

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

[英]How can I find the SQLite file using documentDirectory of Core Data?

我遇到一個問題,每次嘗試使用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

改試試看:NSURL * documentDirectory = [[fileManager URLsForDirectory: NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM