1

RLMClearRealmCache移行をテストするテスト間で Realm の状態をクリアするために使用しています。キャッシュがクリアされていない場合、レルム フィクスチャ ファイル (古いスキーマを持つ) を削除して置き換えても、キャッシュは引き続きスキーマが最新であることを報告するため、次のテストでは移行が実行されません。

RLMClearRealmCache最近、Objective-C++ ファイルに移動されたので、その使用を停止し、プロジェクトでの Objective-C++ の使用を避けたいと考えています。これはまだ最善/唯一の方法ですか?

明確にするために、これらの仕様にはメモリ内 Realm を使用していません。特定のリリースのデバイスから保存したdefault.realmフィクスチャ ファイルがあり、それを使用するために次のことを行っています。

- (void)loadBundledRealmWithName:(NSString *)name;
{
    [self deleteOnDiskRealm];

    // copy over the file to the location
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *source = [[NSBundle bundleForClass:[self class]] pathForResource:name ofType:@"realm"];
    if (documentsDirectory && source) {
        NSString *destination = [documentsDirectory stringByAppendingPathComponent:kDefaultRealmFileName];
        [[NSFileManager defaultManager] copyItemAtPath:source toPath:destination error:nil];
    }
}

ただし、テスト ケース間で を呼び出さないと、Realm のキャッシュは、ファイルをスワップ アウトして再度実行する必要があるRLMClearRealmCacheにもかかわらず、移行が既に実行されていると判断したように見えます。.realm

4

2 に答える 2

0

参照されなくなった場合に Realm がキャッシュをクリアするという事実を利用して、最終的に Realm のキャッシュをクリアするようにしました。それを妨げている問題を突き止めるのは、少しトリッキーでした: テストの実行間で Realm オブジェクトへの参照を保持していました:

context(@"some context", ^{
    __block MyRealmObject *whoops;

    beforeEach(^{
        [specHelper loadBundledRealmWithName:@"release-50-fixture.realm"];
        [migrationManager performMigrations];
        whoops = [[MyRealmObject allObjects] firstObject];
    });

    it(@"first", ^{
        // migrations will run for this `it`
    });

    it(@"second", ^{
        // migrations will NOT run for this `it` since the old Realm is still around and its cache thinks migrations have already run (even though we've swapped out the backing DB).
        // the issue is that `whoops` is retaining a `MyRealmObject` and thus the Realm.
    });
});
于 2016-03-01T23:51:42.597 に答える