2012-04-18 55 views
6

我有一个(proably)简单的解决问题,我coredata持久化存储。使用url dummyURL.sqlite重命名persistentstore协调URL

我这样做在第一天与项目工作

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

,又忘记将其重命名..现在我所有的电流测试设备:

我创造了它与(4)已使用超过2个月,使用应用程序,收集大量的数据,并存储它与一个愚蠢的网址^^

UPDATE我做了一些研究迁移的持久存储和写这个函数:

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

} 

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

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

回答

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

} 

事后我只是改变了appendURL到database.sqli在我的appDelegate。 的作品就像一个魅力:)

0

我会建议创建新的URL第二持久性存储和添加一个按钮的地方,将所有的数据必须进入新的一个。 确保你测试,以确保所有的数据是在新的持久存储您从应用程序的旧人之前。

+0

感谢您的快速答复。我对persistentstore概念有点黑幕(这就是为什么我没有创建后触摸它了)...... 即时通讯目前正在探索'migratePersistentStore'但恐怕实际尝试,因为它dataloss ..:/ 会在哪里我把一个迁移代码? – 2012-04-18 16:23:19