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.

Enumerating on collections via forEach should be implicitly safe to mutations

See original GitHub issue

When you are in a write transaction and enumerate on a collection, this should implicitly take place on a snapshot, so that it is safe to making mutations while enumerating. Otherwise objects would be removed when they aren’t matching the query anymore. e.g.

let dogs = realm.objects("Dog").filtered("age < 5");
dogs.forEach((dog) => {
  dog.age = 6;
});

Given this query would contain two instances, this would fail in the second call of the enumeration block, because the first instance wouldn’t match the query anymore.

/c @alazier @kristiandupont

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
knethcommented, Aug 31, 2018

@Udbhav12 snapshot() doesn’t copy any data but you are keeping a particular version of the database (Realm is MVCC) so it will have an impact on memory and the size of the Realm file.

1reaction
knethcommented, Aug 28, 2018

snapshot() was introduce to solve this problem. You can also use the property isValid on an object to see if it is safe to access it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - IEnumerable<T> thread safety? - Stack Overflow
Yes it's only a problem if the list gets mutated. But note than IEnumerable<T> can be cast back to a list and then...
Read more >
C#: Enumerating collections that change - TechNet Articles
What you should do in this cases like this is to simply replace the foreach loop with a for loop and iterate through...
Read more >
Consider using iterator transforms instead of explicit loops
As with Item 3's discussion of Option and Result , this Item will attempt to show how these iterator transforms can be used...
Read more >
Fundamentals - Collections
Thread-safe collections. Enumeration. The ability to traverse a collection whether it is a simple collection like an array or a complex one like...
Read more >
C# foreach Loop Examples - Dot Net Perls
The foreach loop has a drawback. It restricts any mutations made to the collection during the loop. This can impact code that uses...
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