繁体   English   中英

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

[英]rename persistentstore coordinator URL

我有一个(可以)简单的修复我的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;
}

使用url 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?

我设法使用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 奇迹般有效 :)

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

暂无
暂无

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

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