5

移行しようとしています

モデルには2つのバージョンがあります

1.xcdatamodel
2.xcdatamodel

バージョン1から2へのマッピングモデルを作成しました

1to2.xcmappingmodel

問題は、私が作成した移行モデルが見つからないため、mappingModelが常にnilになることです。使用するmappingModelを指定するために必要なことはありますか?

target = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];
//target and source are initialized correctly
mappingModel = [NSMappingModel mappingModelFromBundles:nil forSourceModel:source destinationModel:target];
4

4 に答える 4

5

マッピングモデルの作成後にモデルの1つを変更した可能性があります。

変更が適切でないと思われる場合でも、適切なマッピングモデルを見つけるために使用されるモデルのハッシュ値が変更されます。少なくとも私は今これに噛まれています:-)

于 2011-12-09T16:15:56.277 に答える
4

1.xcdatamodelから 2.xcdatamodel へのマッピング モデルを既に作成し、適切に構成している場合は、次のようなことができるはずです。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
    {
    if (persistentStoreCoordinator)
        return persistentStoreCoordinator;

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyStore.sqlite"]];

    NSError *error = nil;
   persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                             [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];

   if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
                                        configuration:nil
                                        URL:storeUrl
                                        options:options
                                        error:&error])
        {
        // Handle error
        NSLog(@"Error adding persistent store...%@", error);
        // Handle the error. 
        NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
        NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
        if(detailedErrors != nil && [detailedErrors count] > 0)
            {
            for(NSError* detailedError in detailedErrors)
                {
                NSLog(@"  DetailedError: %@", [detailedError userInfo]);
                }
            }
        else
            {
            NSLog(@"  %@", [error userInfo]);
            }

        }
    else
        {
        DLog(@"Persistent store added without incident, apparently.");
        }

    return persistentStoreCoordinator;
    }
于 2011-04-14T06:04:42.057 に答える
0

元の質問に答えるために、コードは問題ないように見えますが、nil を bundles パラメーターとして渡した理由はわかりません。ドキュメントには、できるとは書かれていません。そう:

NSArray *theBundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
    mappingModel = [NSMappingModel mappingModelFromBundles:theBundles
                                            forSourceModel:source 
                                          destinationModel:target];
于 2011-05-06T19:07:58.257 に答える
0

bundle パラメータとして nil を渡すと、[NSBundle mainBundle] がかかります。

【エリーゼ・ファン・ロイジの質問への回答】

于 2012-12-20T16:07:22.983 に答える