Search code examples
c#xamarinrealm

How to connect to Realm Object Server from .NET code (Xamarin)?


I have a running Realm Object Server on my Mac (192.168.100.61:9080 ip), and Xamarin App on PC. I want to save/synchronize data on the ROS. How I can to connect to ROS from code?


Solution

  • You need to login/register a user and open a synchronized Realm. Something like:

    // pass createUser: true to register rather than login
    var credentials = Credentials.UsernamePassword("[email protected]", "super-secure", createUser: false);
    
    // Login the user
    var user = await User.LoginAsync(credentials, new Uri("http://192.168.100.61:9080"));
    
    // Create a sync configuration for the Realm
    // Notice the URL uses the realm scheme and not http
    var config = new SyncConfiguration(user, new Uri("realm://192.168.100.61:9080/~/myrealm"));
    
    // Finally, get the Realm instance
    // This Realm will be kept in sync with the remote one
    var realm = Realm.GetInstance(config);
    

    For more details, check out the documentation.