We're in the process of converting a project to use Realm. We're really impressed so far especially with the Realm Browser (so handy!).
As a result, a few questions have come up and we'd like to get some concrete usage patterns down before going any further. Our app is heavily multi threaded (API calls, animations, etc), so keep that in mind when reading the questions, since I know Realm instances cannot be accessed across threads (currently).
- How worried should we be about repeatedly creating instances of Realm? What is the overhead?
- Should we bother retaining Realm instances in ViewControllers or Singletons for repeated use? We've tried this but sometimes the instances are accessed from different threads, so we had to revert back to creating a new instance every time.
- When accessing relationship properties on Realm instances, is resulting data that is read retained in memory or is it read from disk every time? Do we have to worry about retained Realm instances becoming too large due to deep relationship access?
When is refreshing a Realm instance necessary? I've noticed that when I make changes in the Realm browser they are reflected in a retained Realm without calling refresh.- Looks like there's a Auto-Refresh property on each realm that causes this according to the documentation.
- Is accessing the
realm
property on anObject
bad practice? We've used this for writing to a Realm if the function using the object didn't create the object or the Realm (on the same thread of course).
For example...
func saveStuff(thingToUpdate: Object?) {
if let thingToUpdate = thingToUpdate, let realm = thingToUpdate.realm {
realm.write {
thingToUpdate.name = "lionpants"
}
}
}
Thanks in advance. I look forward to your answers. :D