Realm 如何用从iCloud下载的另一个域替换默认域

Realm 如何用从iCloud下载的另一个域替换默认域,realm,Realm,我的应用程序使用CloudKit备份领域数据,并在用户按下某个按钮时获取数据。 我用下面的代码将“default.realm”文件从documents文件夹上传到iCloud。 数据似乎上传得很好 // create file path from app's documents folder NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) o

我的应用程序使用CloudKit备份领域数据,并在用户按下某个按钮时获取数据。 我用下面的代码将“default.realm”文件从documents文件夹上传到iCloud。 数据似乎上传得很好

// create file path from app's documents folder
NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* fileName = @"default.realm";
NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:kRecordName];
CKRecord *record = [[CKRecord alloc] initWithRecordType:kRecordType recordID:recordID];

if ([[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
    CKAsset *asset = [[CKAsset alloc] initWithFileURL:[NSURL fileURLWithPath:fileAtPath]];
    [record setObject:asset forKey:@"Realm"];
}

// upload
CKDatabase *privateDatabase = [[CKContainer defaultContainer] privateCloudDatabase];
[privateDatabase saveRecord:record completionHandler:^(CKRecord *record, NSError *error) {
    // completion handling
}
下面是下载部分。我也可以下载,但似乎领域文件没有被替换

CKDatabase *privateDatabase = [[CKContainer defaultContainer] privateCloudDatabase];
CKRecordID *recordID = [[CKRecordID alloc] initWithRecordName:kRecordName];

// fetch realm from cloud
[privateDatabase fetchRecordWithID:recordID completionHandler:^(CKRecord *record, NSError *error) {
  if (!error) {
        CKAsset *realmAsset = record[@"Realm"];
        NSData *realmData = [NSData dataWithContentsOfURL:realmAsset.fileURL];
        NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString* fileName = @"default.realm";
        NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

        // delete existing default.realm file in documents
        if ([[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
            [[NSFileManager defaultManager] removeItemAtPath:fileAtPath error:&error];
        }
        // create a new default.realm file with downloaded data
        if([[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:realmData attributes:nil]) {
              // code goes thru here but nothing changes
        }
    }
}];
我想替换整个领域文件。我做错了什么?我应该从哪里开始?
任何建议都将不胜感激。

若要更改域文件路径,请将路径设置为
Realm.Configuration.fileURL

let fileURL = ...
let config = Realm.Configuration(fileURL: fileURL)

let realm = try! Realm(configuration: config)
如果不希望每次都指定文件路径,可以将配置对象设置为默认配置

let fileURL = ...
let config = Realm.Configuration(fileURL: fileURL)

Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()
另见

基于这些,在运行时交换领域文件是非常危险的。要实现这一点,您必须保证根本没有领域的强引用。相反,您可以创建另一个文件并切换到使用它

// fetch realm from cloud
[privateDatabase fetchRecordWithID:recordID completionHandler:^(CKRecord *record, NSError *error) {
  if (!error) {
        CKAsset *realmAsset = record[@"Realm"];
        ...

        // Do not delete existing default.realm file in documents

        // create a new default.realm file with downloaded data
        // as deferent file name

        NSString* newFileName = @"default1.realm"; // default2.realm, default3.realm, ...
        NSString* newfileAtPath = [filePath stringByAppendingPathComponent:newfileAtPath];
        if([[NSFileManager defaultManager] createFileAtPath:newfileAtPath contents:realmData attributes:nil]) {
              // Switch to using new file to create Realm instance
              // e.g. set new file path to de default configuration file URL
              let config = Realm.Configuration(fileURL: NSURL(fileURLWithPath: newfileAtPath)!)
              Realm.Configuration.defaultConfiguration = config
              ...
        }

按照您的指导,我可以在不更改名称的情况下交换领域文件。似乎没有有力的证据。非常感谢你,你救了我一天!