Hi all!
I’m developing an iOS app using Realm. I read some docs to understand the functioning of Realm data management, especially with the Sync functionality. However I still have a lack of understanding on some points.
My app allows users to choose different items from different categories and then generate some reports containing details about the selected items.
Here are two use cases happening in my app:
Use case #1
To select the items, the user navigate through different categories.
Let’s say I have only 2 levels on categories, the user goes to the category 1, then 1.1 which displays a list of items, then it goes to category 2 → 2.1 which displays a list of other items and so on.
Currently each time a category is opened, I call the following function which open a Realm and then call the function initUserItemsObserver()
which init an observer which do some UI stuffs.
private func initUserItemsRealm(){
self.userItemRealm = try ! Realm(configuration: RealmConstants.USER!.configuration(partitionValue: <partition_value>))
self.userItems = self .userItemRealm?.objects(Items. **self** ).sorted(byKeyPath: "name")
self .initUserItemsObserver()
}
I want to know if there is a way to optimize my algorithm in terms of number of request and sync time because I have categories with hundreds/thousands of items.
Use case #2
Once a report is generated, I save it in a collection. The report object contains an array of _id
corresponding to the one of the items.
When a user wants to see an old report, I use the _id
of the items stored in the array to get the item objects and then generate the report.
My first question is: In that case, to get the item objects from their _id is it better to open a Realm with an observer (as in the initUserItemsRealm()
) or to use AsyncOpen ?
My second question is: In order to optimize the number of requests and the time, I was thinking of storing the information needed from the items directly in the report object (instead of storing only the _id
in the array). Instead of storing directly the information in the report object, I was thinking of using the One-to-Many Relationship to access the information needed from the item objects. However, I read that when using the Relationships « Realm Database executes read operations lazily as they come », but as I will read all the item objects is there a benefit of using the Relationship?
My last question is about the lifecycle of the objects. As explained in the « Think Offline-first » paragraph, the changes received from the server are integrated into the local realm. If I understand well, does it mean that after the objects have been downloaded once to generate the report, if the user wants to generate the same report again, realm will take the objects from the local file instead of downloading them from the server?
Thanks for your help!