Copying Local Realm to Sync Realm, and vice versa

cloud

#1

I have used Local Realm for a long time in my app. And I’m going to use Sync Realm for a back up feature.(I’m not familiar with Sync Realm). The back up feature will be one of the paid features. So a user should be able to switch between Local Realm and Sync Realm. So I wrote some sample code for copying Local Realm to Sync Realm, or vice versa. (Swift)

The things I’d like to ask are, (1) the code below is a correct way or not, and (2) I’m also concerned whether I should take care of schema version in this situation or not.

Any advice and suggestions will be greatly appreciated.

// Properties
let syncRealm: Realm = try! Realm(configuration: SyncUser.current!.configuration())
let localRealm: Realm = try! Realm()


// [A] Copy Local Objects to Sync Objects (Merge Pattern)
// *A project that has an existing primary key will be updated
// Use this in case you want to keep local ones
try! syncRealm.write {
    let permissionUser: PermissionUser = syncRealm.object(ofType: PermissionUser.self, forPrimaryKey: SyncUser.current!.identity!)!

    localRealm.objects(Project.self).forEach { (localProject) in
        let syncProject: Project = syncRealm.create(Project.self, value: localProject, update: true)
        let permission: Permission = syncProject.permissions.findOrCreate(forRole: permissionUser.role!)
        permission.canRead = true
        permission.canUpdate = true
        permission.canDelete = true
    }
}



// [B] Copy Local Objects to Sync Objects (Replace All Pattern)
try! syncRealm.write {

    let permissionUser: PermissionUser = syncRealm.object(ofType: PermissionUser.self, forPrimaryKey: SyncUser.current!.identity!)!
    syncRealm.delete(syncRealm.objects(Project.self))

    localRealm.objects(Project.self).forEach { (localProject) in
        let syncProject: Project = syncRealm.create(Project.self, value: localProject, update: true)
        let permission: Permission = syncProject.permissions.findOrCreate(forRole: permissionUser.role!)
        permission.canRead = true
        permission.canUpdate = true
        permission.canDelete = true
    }
}



// [C] Copy Sync Objects to Local Objects (Merge Pattern)
// *A project that has an existing primary key will be updated
// Use this in case you want to keep local ones
try! localRealm.write {
    syncRealm.objects(Project.self).forEach { (syncProject) in
        // Local Realm throw an exception when using Permission which has SyncUser so create new object.
        let localProject = Project()
        localProject.id = syncProject.id
        localProject.name = syncProject.name
        localProject.timestamp = syncProject.timestamp
        syncProject.items.forEach { (item) in
            localProject.items.append(item)
        }

        localRealm.create(Project.self, value: localProject, update: true)
    }
}



// [D] Copy Sync Objects to Local Objects (Replace All Pattern)
try! localRealm.write {
    localRealm.delete(localRealm.objects(Project.self))
    syncRealm.objects(Project.self).forEach { (syncProject) in
        // Local Realm throw an exception when using Permission which has SyncUser so create new object.
        let localProject = Project()
        localProject.id = syncProject.id
        localProject.name = syncProject.name
        localProject.timestamp = syncProject.timestamp

        syncProject.items.forEach { (item) in
            localProject.items.append(item)
        }

        localRealm.create(Project.self, value: localProject, update: true)
    }
}