Slow In realm.add and increasing memory


We use the realm on our project.
But at first time we need to insert into local database a mass of 7,000 entries with some attributed each entry.
The code has taking 4min to finish the process, also the memory has exponential growing up to 20gb (in simulator).
Here is an example from the log:

09-04-2019 10:11:29.251 [Debug] [CRMService.swift:36] updateCRMIfNeeded(completion:) > CRM sync: Start
09-04-2019 10:15:02.678 [Debug] [CRMService.swift:36] updateCRMIfNeeded(completion:) > CRM sync: Finished. Error - ni

My code:

try realm.write {
    . // Fetch into the person class

    if isCreateNew {

Please, how can I do it, with best performance?

  1. Please profile what is taking the time. A bare minimum experiment is to just in the above define a person with some static data. Then you know how long it takes to enter dummy data for 7000 entries.

  2. If it’s static data that you can generate before you ship the app, you can include the generated realm file with the app so it’s available at startup.


Also, just by looking at the code you posted, it appears that you’re creating a single object per transaction. Creating all 7000 objects and then persisting them in a single transaction will be more efficient. It’s still highly unlikely that Realm is taking up the memory and the cpu time unless your 7000 objects contain large binary data.