2012-04-18 7 views
6

私はcoredata永続ストアで問題を簡単に解決できます。名前永続ストアコーディネータURL

私はそれを作成した:私はすべての私の現在のテストデバイスになりました。..プロジェクトでの作業初日にこれをしなかったし、その名前を変更するのを忘れているURLにdummyURL.sqlite

を使用して

- (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)大量のデータを収集し、愚かなURLでそれを格納し、アプリケーションを使用して、2ヶ月以上のために使用されていた^^

UPDATE私は、永続ストアの移行に関するいくつかの研究を行なったし、この機能を書いた:私はそのコピーがすべての新しいURLと二永続ストアを作成し、どこかにボタンを追加することをお勧めし

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

} 

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

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

} 

私はちょうど私のappDelegateにdatabase.sqliにappendURLを変更しました。 は魅力的に機能します:)

0

あなたが新しいものに持っているデータ。 古い永続ストアをアプリケーションから削除する前に、すべてのデータが新しい永続ストアにあることを確認するテストを行ってください。

+0

クイックアンサーに感謝します。 私は現在 'migratePersistentStore'を探していますが、実際にはdatalossのためにそれを試すことを恐れています。:/ どこに置くでしょうか?(私はそれを作成した後、もうそれを触れていませんでした)移行コード? –

関連する問題