繁体   English   中英

无法在IOS5中使用核心数据库

[英]Can't use core database in IOS5

我正在使用核心数据库,它在IOS 6中正常工作,但是当我尝试在IOS 5上对其进行测试时,它没有任何作用。 让我解释一下我在做什么。

首先,我在viewWillAppear中执行此操作。

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"view appeared");
    [super viewWillAppear:animated];
    if (!self.genkDatabase) {
        NSLog(@"comes to here");
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Default appGenk Database2"];
        // url is now "<Documents Directory>/Default Photo Database"
        self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
        NSLog(@"database created on disk");
    }

}

然后它进入UseDocument方法。

- (void)useDocument
{
    NSLog(@"Comses in the use document");
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
        // does not exist on disk, so create it
        [self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"create");
            [self setupFetchedResultsController];
           [self fetchGenkDataIntoDocument:self.genkDatabase];

        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
        NSLog(@"closed news");
        // exists on disk, but we need to open it
        [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
            [self setupFetchedResultsController];
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
        NSLog(@"normal");
        // already open and ready to use
        [self setupFetchedResultsController];
    }

}

最后,它进入setDatabase方法。

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
    NSLog(@"Comes in the setdatabase methode.");
}

完成所有操作后,将显示以下日志。

2012-10-22 10:42:47.444 RacingGenk[4786:c07] view appeared
2012-10-22 10:42:47.445 RacingGenk[4786:c07] comes to here
2012-10-22 10:42:47.459 RacingGenk[4786:c07] Comses in the use document
2012-10-22 10:42:47.460 RacingGenk[4786:c07] Comes in the setdatabase methode.
2012-10-22 10:42:47.461 RacingGenk[4786:c07] database created on disk

就像您看到的那样,它不会在我的使用文档中打印创建日志。 因此,它无法执行FetchDataIntoDocument方法。

任何人都可以帮助我解决这个问题。 我正在寻找这个问题很久了。

非常感谢。

燕姿

您正在使用模拟器进行测试还是在设备上进行测试? 模拟器不区分大小写-设备在文件访问时区分大小写,请记住!

但是除此之外NSFileManager的文档建议不要检查文件是否存在,而只是尝试读取文件并优雅地处理所有错误(例如,找不到文件错误)。 因此,只需尝试加载文件,而不要检查文件是否存在。

而且我没有看到您的数据库文件的任何文件扩展名! 它只是说“默认的appGenk Database2”。到目前为止,这已经是文件名还是突出了另一个子目录?

编辑:

您可以尝试以下代码:

- (void) setupStore {
    NSString* storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"mydatabase.sqlite"];

    // Set up the store.
    NSFileManager* fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, create one.
    if (![fileManager fileExistsAtPath: storePath]) {
        // create your store here!
    }
}


- (NSString*) applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

“ mydatabase.sqlite”是您希望出现在文档目录中的数据库文件的名称。

如果您想查看有关如何设置核心数据持久性存储的完整示例,可以查看苹果自己的iPhoneCoreDataRecipes示例。 只是看看

RecipesAppDelegate.m

实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM