Search code examples
iosiphoneswiftrealm

Is it a bad idea to create a model that deals with realm db and a model for use


I just started using realm in my ios swift project, and noticed several issues related to accessing realm objects from different threads.

So I decided to have, for each model, an associate realm model.

For example, for my User model, I will have UserRealm model which has the same attributes. However, I use UserRealm to read/write the db, and User to use through out the App.

Is this a bad idea? Does it affect the efficiency of realm?


Solution

  • Yes, copying the objects back and forth will obviously cost performance. If your model is relational with links between the objects, you are also likely to lose those as you copy out individual objects.

    What kinds of threading issues do you see? As long as you handover the objects between the threads you should be safe. Check out https://realm.io/docs/swift/latest/#passing-instances-across-threads for examples.