Search code examples
iosswiftsynchronizationrealm

Realm Swift: Question about Query-based public database


I’ve seen all around the documentation that Query-based sync is deprecated, so I’m wondering how should I got about my situation:

In my app (using Realm Cloud), I have a list of User objects with some information about each user, like their username. Upon user login (using Firebase), I need to check the whole User database to see if their username is unique. If I make this common realm using Full Sync, then all the users would synchronize and cache the whole database for each change right? How can I prevent that, if I only want the users to get a list of other users’ information at a certain point, without caching or re-synchronizing anything?

I know it's a possible duplicate of this question, but things have probably changed in four years.


Solution

  • The new MongoDB Realm gives you access to server level functions. This feature would allow you to query the list of existing users (for example) for a specific user name and return true if found or false if not (there are other options as well).

    Check out the Functions documentation and there are some examples of how to call it from macOS/iOS in the Call a function section

    I don't know the use case or what your objects look like but an example function to calculate a sum would like something like this. This sums the first two elements in the array and returns their result;

    your_realm_app.functions.sum([1, 2]) { sum, error in
       if let err = error {
          print(err.localizedDescription)
          return
       }
    
       if case let .double(x) = result {
          print(x)
       }    
    }