【问题标题】:Prepopulate Core Data in iOS 5在 iOS 5 中预填充核心数据
【发布时间】:2011-12-23 21:15:26
【问题描述】:

NSPersistentStoreCoordinator 方法似乎有一些修改是 iOS 5。 我正在尝试获取一个预填充的数据库......它似乎不起作用,没有崩溃,但似乎没有任何数据存在......有什么建议吗?

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storePath = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"];

    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:[storePath absoluteString]]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:[storePath absoluteString] error:NULL];
        }
    }


    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return __persistentStoreCoordinator;
}

【问题讨论】:

  • 我不能告诉你为什么这不起作用,但我可以告诉你完全相同的代码对我有用。 sqlite 数据库是否已复制到您的 Documents 文件夹中?
  • @user1028028 嘿,你得到了很好的答案。你为什么不批准 Ivo Leko 的回答?

标签: objective-c core-data ios5


【解决方案1】:

问题出在applicationDocumentsDirectory 方法中,它给出了错误的绝对字符串。确实没有错,但不适合NSFileManager类。

解决方案很简单。你需要编写另一个方法,我称之为applicationDocumentsDirectory2

首先,转到您的 AppDelegate 的头文件并声明方法:

- (NSString *)applicationDocumentsDirectory2;

现在,转到 .m 文件,并定义一个方法:

- (NSString *)applicationDocumentsDirectory2 {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

最后,只需将persistentStoreCoordinator 方法更改为:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }
    NSError *error = nil;

   NSString *storePath = [[self applicationDocumentsDirectory2] stringByAppendingPathComponent: @"DataModel.sqlite"];

    /*
     Set up the store.
     For the sake of illustration, provide a pre-populated default store.
     */
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"sqlite"];
        if (defaultStorePath) {
            if(![fileManager copyItemAtPath:defaultStorePath toPath:storePath error:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);


            }
        } 
    }


    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DataModel.sqlite"];

    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return __persistentStoreCoordinator;

}

就是这样! :)

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 2012-01-30
    • 2011-04-27
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多