How to replace default Realm with another Realm downloaded from iCloud
<p>My app uses CloudKit to backup the realm data and fetch the data whenever user press a certain button.
I coded to upload "default.realm" file from documents folder to iCloud with below code.
And the data seems to be uploaded well.</p>

<pre><code>// 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
}
</code></pre>

<p>And below is the download part. I can download as well but seems like the realm file is not replaced.</p>

<pre><code>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:&amp;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
        }
    }
}];
</code></pre>

<p>I want to replace the entire realm file. What am I doing wrong? Where should I start from?
Any advice will be appreciated.</p>