如何将默认Realm替换为从iCloud下载的另一个Realm

时间:2016-07-26 20:53:51

标签: realm

我的应用程序使用CloudKit备份领域数据,并在用户按下某个按钮时获取数据。 我编码上传" default.realm"使用以下代码从文档文件夹到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
        }
    }
}];

我想替换整个领域文件。我究竟做错了什么?我应该从哪里开始? 任何建议将不胜感激。

1 个答案:

答案 0 :(得分:1)

要更改Realm文件路径,请设置为private void initializeStreams() { try { input = new ObjectInputStream(socket.getInputStream()); if (input != null) { System.out.println("ObjectInputStream successfully initiated"); } else { System.out.println("ObjectInputStream is null but did not return an exception when being instantiated"); } } catch (IOException ioe) { System.out.println("Could not initialize ObjectInputStream: " + ioe.getMessage()); } try { output = new ObjectOutputStream(socket.getOutputStream()); if (output != null) { System.out.println("ObjectOutputStream successfully initiated"); } else { System.out.println("ObjectOutputStream is null but did not return an exception when being instantiated"); } } catch (IOException ioe) { System.out.println("Could not initialize ObjectOutputStream: " + ioe.getMessage()); } }` 的路径。

Realm.Configuration.fileURL

如果您不想每次都指定文件路径,可以将配置对象设置为默认配置。

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

let realm = try! Realm(configuration: config)

另见https://realm.io/docs/swift/latest/#realm-configuration

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

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

Realm.Configuration.defaultConfiguration = config

let realm = try! Realm()