传输到ipad时,填充的sqlite db为空

时间:2011-08-01 00:27:06

标签: xcode ipad sqlite core-data xcode4

我有一个核心数据应用程序,我正在使用sql lite db。我已经在我的模拟器中填充了它,现在我想用应用程序发送它,但是每当我在ipad上安装它时,数据库都是空白的!

我已将填充的数据库添加到项目中(来自模拟器文件夹),我在委托中有以下内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[window addSubview:splitViewController.view];
[window makeKeyAndVisible];

NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:@"data.plist"];

NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:DataPath];
self.data = tempDict;
[tempDict release];

// Override point for customization after application launch.
// Add the split view controller's view to the window and display.
[self createEditableCopyOfDatabaseIfNeeded];


NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithObject:@"NO" forKey:@"enableRotation"];
[appDefaults setObject:@"0" forKey:@"orientation"];
[appDefaults setObject:@"100" forKey:@"repID"];
[appDefaults setObject:@"Atlanta Dental" forKey:@"RepName"];
[defaults registerDefaults:appDefaults];

//self.window.rootViewController = self.splitViewController;
return YES;
}

-(void)createEditableCopyOfDatabaseIfNeeded
{
// Testing for existence
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                     NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];

success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
    return;

// The writable database does not exist, so copy the default to
// the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
                           stringByAppendingPathComponent:DATABASE_NAME];
success = [fileManager copyItemAtPath:defaultDBPath
                               toPath:writableDBPath
                                error:&error];
if(!success)
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    //if ( ! [fileManager fileExistsAtPath:storePath])
    //{
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"D4" ofType:@"sqlite"];
        if ( defaultStorePath )
        {
            [fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL];
        }
    //}
    //NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
    //        [error localizedDescription]);
}
}


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

NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:DATABASE_NAME];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}    

return persistentStoreCoordinator;
}

有什么想法吗?

编辑添加:

当我使用iphone explorer浏览iPad时,我可以在应用程序路径中看到原始填充的数据库,因此它会被复制。这意味着我必须犯上述错误。 。 。

正确?

2 个答案:

答案 0 :(得分:1)

由于商店总是空白,这意味着持久性商店协调员在您提供的URL处找不到现有商店,因此它会创建一个新的空商店。丢失商店最可能的解释是它没有被复制到你认为被复制的地方。

您永远不会使用相同的方法两次来获取应用程序包中的默认文件或文档中的readwrite文件的路径。每次创建路径时,都使用不同的代码。这使您可以在代码中的不同点生成不同的路径。选择一种生成每条路径的方法,并在必要时重复使用。

我建议记录所有路径和网址,以确认它们都指向您认为的位置。我还建议为文件管理操作捕获错误。即使bool返回为YES,您也可能会收到错误。

答案 1 :(得分:0)

这是一个解决方案。通过从模拟器复制填充的db来将其复制到项目中。然后从设备中删除应用程序并进行全新安装,然后应用程序将另外复制填充的数据库,如果空白数据库已经存在,那么它将不会再次复制数据库。请参阅您的代码

NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];

success = [fileManager fileExistsAtPath:writableDBPath];
if (success)//database already copied
    return;