Copying over local Realm to Synced Realm


#1

Hello,

I’m introducing Realm Platform along with authentication to my application soon (hopefully). Users have been so far using a local Realm database only.

My plan is as follows: Upon successful login, I would like to copy my local Realm over to Sync Realm but it doesn’t seem to work. I’ve been looking at demos and posts but I either get an error or a warning that one of the methods I’m using is deprecated.

Here is my code:

SyncUser.logIn(with: credentials, server: Constants.AUTH_URL, onCompletion: { [weak self](syncUser, err) in
                      
                                if let syncUser = syncUser {
                                    
                                    DispatchQueue.main.async {
          
                                        var syncedConfiguration = syncUser.configuration(realmURL: Constants.REALM_URL, fullSynchronization: true, enableSSLValidation: true, urlPrefix: nil)
                                        syncedConfiguration.objectTypes = [Note.self, NoteList.self, PinnedListsPointer.self, Section.self, User.self]

                                        let syncedRealm = try! Realm(configuration: syncedConfiguration)

                                        try! syncedRealm.write {
                                            syncedRealm.create(FoldersPointer.self, value: foldersPointer, update: .error)
                                            syncedRealm.create(Folder.self, value: folder, update: .error)
                                            syncedRealm.create(FolderItem.self, value: folderItem, update: .error)
                                            // [...]
                                            syncedRealm.create(Subtask.self, value: subtask, update: .error)
                                            syncedRealm.create(User.self, value: userObject, update: .error)

                                        }
                       } 
       }
})

Any help would be appreciated.


#2

I think more info is needed to figure out what is wrong. Any stack trace? The code sample you wrote only seems to cover the configuration and not the actual copying data?


#3

I added more code.

When I execute the second part of the code I get a weird error:

*** Terminating app due to uncaught exception 'RLMException', reason: 'Invalid value '(
    "dossier ",
    "",
    "",
    "",
    "",
    ""
)' of type '__NSArrayM' for 'string' property 'Folder.name'.'

I found someone else reporting a similar error on Github: https://github.com/realm/realm-cocoa/issues/4516


#4

And when I implement the answer from this stack overflow thread: https://stackoverflow.com/questions/43748387/realm-object-server-sync-initial-local-db

I get the following error: ‘Object is already managed by another Realm. Use create instead to copy it into this Realm.’

EDIT: I managed to get the code to work. I had to replace add() by create(type:value:updatePolicy) and set isAdmin to true. It just seems weird to me that all my users get to have admin privileges.