在iOS 5中预填写核心数据 [英] Prepopulate Core Data in iOS 5

查看:107
本文介绍了在iOS 5中预填写核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有些修改 NSPersistentStoreCoordinator 方法是iOS 5.
我想获得一个预填充的数据库...它似乎工作,没有崩溃,但没有数据似乎存在...任何建议?

There seem to be some modifications to the NSPersistentStoreCoordinator method is iOS 5. I am trying to get a prepopulated database ... it doesn't seem to work, no crash but none of the data seems to exist... Any suggestions?

- (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;
}


推荐答案

问题出在 applicationDocumentsDirectory 方法给出错误的绝对字符串。这不是真的错,但不适合 NSFileManager 类。

The problem is in the applicationDocumentsDirectory method that gives wrong absolute string. It is not really wrong, but is not suited for NSFileManager class.

解决方案很简单。你需要写另一个方法,我调用 applicationDocumentsDirectory2

The solution is simple. You need to write another method which I called applicationDocumentsDirectory2.

首先,去AppDelegate的头文件, declare method:

So first, go to header file of your AppDelegate and declare method:

- (NSString *)applicationDocumentsDirectory2;






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


Now, go to the .m file, and define a method:

- (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;

}

就是这样! :)

这篇关于在iOS 5中预填写核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆