2012-04-18 50 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