2

I'm implementing android RecycleView using Xamarin and Realm as database. RecycleView requires access to datasource by index. Is there any way to retrieve element by index from RealmResults? I've found that it is possible in realm java just by calling realmResults.get(index) method. But apparently dotnet realization of Realm doesn't have such method.

Also according to the Realm documentation:

Objects are not copied - you get a list of references to the matching objects, and you work directly with the original objects that match your query.

So would it be optimal enough to just call .ToList() on realmresults and use this collection as datasource?

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
Semyon
  • 528
  • 5
  • 16

2 Answers2

1

Realm-Xamarin docs says:

To extract a list of all users named John or Peter you would write:

var johnsAndPeters = realm.All<Person>().Where(p => 
  p.FirstName == "John" || 
  p.FirstName == "Peter"); 
var peopleList = johnsAndPeters.ToList();

The ToList call, in this example, fires off the query which maps straight to the Realm core.

Objects are not copied - you get a list of references to the matching objects, and you work directly with the original objects that match your query.

Essentially yes, by calling ToList() you obtained what is essentially RealmResults<T> in Realm-Java.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • Umm NO! ToList() will iterate the RealmResults IMMEDIATELY and pull all the objects into memory. It is not copying objects but it will immediately instantiate all of them. A RealmLiist will only instantiate as you access each in turn. – Andy Dent Jul 27 '16 at 16:44
  • Sorry, that ToList approach will probably work well enough for a lot of sizes of lists - I just reacted a bit quickly to the factual inaccuracies. It may be a good enough workaround for now. – Andy Dent Jul 27 '16 at 17:12
1

We have added an issue to implement this exposed as ElementAt.

Andy Dent
  • 17,578
  • 6
  • 88
  • 115