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

URLを使用してdummyURL.sqlite

私はプロジェクトで作業した最初の日にこれを行いましたが、名前を変更するのを忘れていました..現在、現在のすべてのテストデバイス (4) は、アプリケーションを使用して 2 か月以上使用され、多くのデータを収集し、馬鹿げた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];

}

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

}

その後、appDelegate で appendURL を変更しましたdatabase.sqli。魅力のように機能します:)

于 2012-04-18T16:38:55.983 に答える
0

新しい URL で 2 つ目の永続ストアを作成し、すべてのデータを新しい URL にコピーするボタンをどこかに追加することをお勧めします。アプリから古い永続ストアを削除する前に、すべてのデータが新しい永続ストアにあることをテストして確認してください。

于 2012-04-18T16:14:22.600 に答える