重命名持久性存储区协调器URL

时间:2012-04-18 15:59:12

标签: iphone objective-c ios core-data

我有一个(可以)简单的修复我的coredata持久性商店的问题。

我创建了它:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{

    if (persistentStoreCoordinator != nil)
    {
        return persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"dummyURL.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;
}

使用网址dummyURL.sqlite

我是在使用该项目的第一天做到这一点,并且忘了重命名..现在我所有现有的测试设备(4)都在使用超过2个月,使用该应用程序,收集大量数据和用愚蠢的网址存储它^^

更新我对持久存储的迁移进行了一些研究,并写了这个函数:

-(void)migrate{

    NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator];
    NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];

    NSError *error = nil;

    NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL];
    NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld
                                                           toURL:newURL
                                                         options:nil
                                                        withType:NSSQLiteStoreType
                                                        error:&error];

}

问题1 will this work or will i lose some data with that?

问题2 afterwards will i just have to change the appendString in my persistenstore function?

2 个答案:

答案 0 :(得分:3)

我设法使用migratePersistentStore函数自己解决它:

-(void)migrate{

    NSPersistentStoreCoordinator *psc = [self.dataHandler.managedObjectContext persistentStoreCoordinator];
    NSURL *oldURL = [psc URLForPersistentStore:[[psc persistentStores]objectAtIndex:0]];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSURL *newURL = [[appDelegate applicationDocumentsDirectory] URLByAppendingPathComponent:@"database.sqlite"];

    NSError *error = nil;

    NSPersistentStore *oldStore = [psc persistentStoreForURL:oldURL];
    NSPersistentStore *newStore = [psc migratePersistentStore:sqliStoreOld
                                                           toURL:newURL
                                                         options:nil
                                                        withType:NSSQLiteStoreType
                                                        error:&error];

}

之后我刚刚在appDelegate中将appendURL更改为database.sqli。 像魅力一样:)

答案 1 :(得分:0)

我建议使用新URL创建第二个持久性存储,并在某处添加一个按钮,将所有数据复制到新数据中。 在从应用程序中删除旧数据之前,请确保进行测试以确保所有数据都在新的持久存储中。