Search code examples
realm

Best approach to having multiple users in one app


This is mobile app which can have different kind of users. I'm using realm only for the offline storage. Say I have two users A and B and a have a List Class. This class wont ever be shared, so different data for each user. How would i go in designing the schema? Considering versioning and migration.

A. Add a primary key for the List and assign it differently to user A and B.

B. Use two different realms


Solution

  • There is no one good way of defining your Realm schema and the solution to choose completely depends on the exact scenario.

    If you want your users data to be completely independent of each other and you will never need to use a single query to retrieve both users data or to access some common data, then using separate Realm instances for each use seems like a good approach. It provides complete separation between your users data.

    However, if your users might have some shared data or if you might end up making some statistics about all of your users even though their data is independent, using a single Realm instance is the way to go. In this case you should just create a one-to-many relationship between each of your users and whatever objects you want to store in your lists like this:

    class User:Object {
        let stuff = List<Stuff>()
    }