Enumerating on collections via forEach should be implicitly safe to mutations
See original GitHub issueWhen 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.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.snapshot()
was introduce to solve this problem. You can also use the propertyisValid
on an object to see if it is safe to access it.