6

我的 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?

4

2 回答 2

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

}

之后我只是将appDelegatedatabase.sqli中的appendURL更改为。奇迹般有效 :)

于 2012-04-18T16:38:55.983 回答
0

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

于 2012-04-18T16:14:22.600 回答