question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Iterating live result during update (write) is slow

See original GitHub issue

Problem

Perfoming the following operations: Given a dataset of 1000 elements, update matching string with a new string Takes a ridiculously huge amount time And the time complexity grows dramatically when increasing the dataset to 10 000 elements

Code snippet

    override suspend fun update(param: String, objectToInsert: NewsReportDto) {
        realm.query<NewsReportDto>("title == $0", param)
            .find()
            .forEach { oldNewsReportDto ->
                realm.write {
                    findLatest(oldNewsReportDto)?.apply {
                        title = objectToInsert.title
                        description = objectToInsert.description
                    }
                }
            }
    }

Link to this update method source code Link to the data generator

Performance result

performance screenshot

Let me know if i’m doing something wrong, the corresponding update query for Room

        "UPDATE newsReport " +
                "SET title = :newTitle," +
                "description = :newDescription " +
                "WHERE title = :title"

takes very little time for example, 20 miliseconds for 1k elements, and 20 miliseconds for 10k elements

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
rorbechcommented, Jun 13, 2022

@konnovdev I have been digging a bit and what happens is that the underlying query is most likely re-run when you update the objects, so for now the best thing is probably to iterate the stable result from the frozen realm.query. I will have to dig a bit further to understand the details and, if the hypothesis is right, the options of providing a stable results for these use cases.

0reactions
rorbechcommented, Jun 28, 2022

Closing this issue as the various root causes of the lower speed has been explained, but created #914 to enhance the API to provide a stable result for fast and predictable iteration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python is slow when iterating over a large list - Stack Overflow
It's probably slow because you load all result in memory first and performing the iteration over a list. Try iterating the cursor instead....
Read more >
Gotcha: Entity Framework gets slow in long Iteration Loops
There are a few simple solutions to this problem: Recreate the dbContext/Business object inside of the loop for each iteration. Turn off change...
Read more >
Apply for each is very slow - Power Platform Community
Hello, I am using Apply for Each to loop through the records and some conditions in LOOP. My observation is that the Flow...
Read more >
Why is iterating over a dictionary slow in Python?
Dictionaries are mutable, and take up more memory than arrays or (named) tuples (when organized efficiently, not duplicating type information).
Read more >
Edit Live Photos on iPhone - Apple Support
In the Photos app , you can edit Live Photos, change the key photo, and add fun effects like Bounce and Loop. Edit...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found