0

このガイドに従って、ドキュメントベースではないアプリケーションにコアデータの移行とバージョン管理を実装しました。報告された運転をしましたが、いつ起動すると、開いているモデルがサポートされていないことがアプリケーションから通知されます。どうすればこれを修正できますか?

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

    NSManagedObjectModel *mom = [self managedObjectModel];
    if (!mom) {
        NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
        return nil;
    }

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
    NSURL *storeURL = [[self applicationFilesDirectory] URLByAppendingPathComponent:@"Preventivi.storedata"];

    NSError *error = nil;
    //Turn on automatic store migration
    NSMutableDictionary *optionsDictionary = [NSMutableDictionary dictionary];
    [optionsDictionary setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
    NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];

    if (!properties) {
        BOOL ok = NO;
        if ([error code] == NSFileReadNoSuchFileError) {
            ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
            NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Preventivi" withExtension:@"storedata"];
            if (defaultStoreURL) {
                [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
            }
        }
        if (!ok) {
            [[NSApplication sharedApplication] presentError:error];
            return nil;
        }
    } else {
        if (![properties[NSURLIsDirectoryKey] boolValue]) {
            // Customize and localize this error.
            NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];

            NSMutableDictionary *dict = [NSMutableDictionary dictionary];
            [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
            error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];

            [[NSApplication sharedApplication] presentError:error];
            return nil;
        }
    }

    NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"Preventivi.storedata"];
    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
    if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
    _persistentStoreCoordinator = coordinator;

    return _persistentStoreCoordinator;
}
4

2 に答える 2

0

自動移行を使用するようにCoreDataスタックを構成したようです。そして、バージョン管理を使用せずにモデルを変更し続けました。

バージョン管理されたモデルを使用する必要があります。変更する前にモデルバージョンを作成しなかった場合、自動移行は機能しません。すべての変更の前に自動移行を機能させるには、新しいモデルバージョンを作成し、その新しいバージョンを変更する必要があります。

たとえば、カップル属性をエンティティに追加し、別のエンティティを追加する場合は、次のようにします。

  1. 新しいモデルバージョンを作成します(モデルファイルを選択し、[エディター]-> [モデルバージョンを追加...])。
  2. その新しいバージョンを選択し、属性とエンティティを追加します。

また、自動移行で構成に問題がある場合は、構成を使用するかどうかも指定する必要があります。

于 2012-12-14T10:34:52.660 に答える
0

アプリケーションがエラーをスローする場所がわかりますか? 最後の行にある場合は、たとえば、storeType NSXMLStoreType を NSSQLiteStoreType に変更してみてください。

if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error])

if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error]) 

このためには、サフィックスを「sqlite」に変更する必要もあります。

NSURL *url = [NSURL fileURLWithPath: [applicationFilesDirectory stringByAppendingPathComponent: @"Preventivi.sqlite"]]

NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Preventivi" withExtension:@"sqlite"];

または、すでに解決策がある場合は、これを共有できます:)しかし、私のコードと比較すると、実際の違いはありません(オプションのみがあります)

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

永続ストアを追加するときにオプションを使用しません^^ )

これが役立つことを願っています:)

于 2012-12-14T10:23:25.897 に答える