Home > Mobile >  offline - Launch Unity with MongoDB Realm
offline - Launch Unity with MongoDB Realm

Time:02-24

I’m building a game in unity and the players' data are stored in a MongoDB Realm DB with Sync enabled. When I launch the game online, it works fine and if I close and re-open, the user is still here.

My problem occurs when I close, set offline mode on my device, and get back: it’s not finding any users. When looking on "set up realm for unity" tutorials, they always talk about the "new PartitionSyncConfiguration" instead of getting the stored one. Being offline is it just not a Realm Sync feature? Hope you’ve got some answers ;)

realmApp = App.Create(new AppConfiguration(AppId)
{
    MetadataPersistenceMode = MetadataPersistenceMode.NotEncrypted
});

if (realmApp.CurrentUser == null)
{
    SceneManager.LoadScene("user_login");
}
else
{
    // Does not work offline
    realmUser = realmApp.CurrentUser;
    realmInstance = await Realm.GetInstanceAsync(new PartitionSyncConfiguration(realmUser.Id, realmUser));
    SceneManager.LoadScene("project_list");
}

CodePudding user response:

request the saved data from the MongoDB in Void Awake () {}. and you can search on youtube Unity MongoDB

CodePudding user response:

So thanks to a MongoDB Employee, who founded me the solution (and guess what, it’s stupid) : https://www.mongodb.com/community/forums/t/offline-launch-unity-with-mongodb-realm/148914

var config = new PartitionSyncConfiguration(realmUser.Id, realmUser);
    try
    {
        var cts = new CancellationTokenSource(10_000); // cancel after 10s
        realmInstance = await Realm.GetInstanceAsync(config, cts.Token);
    }
    catch (TaskCancelledException)
    {
        // GetInstanceAsync timed out, let's fall back to using the local data
        realmInstance = Realm.GetInstance(config);
    }
  • Related