Offline first partitioning strategies

I have an app that doesn’t require user login at the first time, user can login later when they want.
So when an user use the app at the first time (not login), what’s the best way to partitioning so I can use that partition key to sync their local data to Mongo Realm later?

The answer is “it depends” but I suspect using the user-id (or something like partition: "user-id=376473422 may be a good option.

Check out this article on partitioning strategies: Realm Partitioning Strategies

@Andrew_Morgan how can you get the user id that it’s not duplicated when the user login and sync data? In the old Realm, partition key is not required so it’s not a problem

You can use anonymous login initially, and when they later register with email/password you can link the 2 identities. This is for iOS but you can find instructions for other platforms in the docs …

1 Like

@Andrew_Morgan but at the first time, if an user is offline, how can they login as anonymous?

From what I understand, they can’t be assigned a user id unless they connect. It’s a limitation of Realm.

Also, from what I understand, and hopefully @Andrew_Morgan can correct me if I’m wrong, but if they authenticate as anonymous to receive that initial user id, that will expire if they don’t use the app for 90 days. So for instance if your user uses the app for a year or so, then doesn’t delete the app but takes three months off and then comes back to the app, they can no longer access their data.

This Anonymous thing is good in theory and in demo apps, but has quite a ways to go before it is actually useful in production.

Similarly, the offline first or offline later problem isn’t necessarily solved. For instance, if you want to go back to having an offline only experience in the future, there is no way to pause syncing and you have to copy the entire realm and all relationships, which can be data intensive and in the case of incomplete copying due to crashes, memory issues, or impatience on the part of the user, this can introduce all kinds of issues.

I hope you can keep thinking creatively about how to solve this issue though. It is holding me back significantly in my migration to MongoDB Realm from Realm Cloud.

1 Like

They can’t, they need to be online to login at least once, the first time. Generally this is fine since they needed to have an internet connection to install the app to begin with. An alternative to anonymous login is to perform a silent login for them in code and just assign them a random string as a username and then keep a metadata table mapping somewhere so you can identify this user later. This way every user of sync has a username even if they didn’t create a login themselves.

1 Like