问题
I have recently read inside documentation for Realm
database that all of their queries are lazy and I am not sure, if I understand correctly the implications which this may cause.
Let me explain how I understand it and please feel free to correct me if I'm wrong. The way I see it is that, whenever I am making such command mRealm.where(Customer.class).equalTo(Customer.ID, "someId").findFirst();
I do not get Java
object with all filled data, that is contained in db for this customer. Instead queries are made whenever I try to access some fields of this "fetched" object. Therefore I am wondering, if that is faster than OrmLite
, if I want to access all of the fields of given class?
Another question related to it. Is it good idea to use Realm
db for items which would be displayed in ListView
or RecyclerView
? If queries are constantly made during scrolling of the list, wouldn't it have serious impact on the performance?
I would be really glad, if someone could explain this to me in more detail.
回答1:
RealmResults, which are created by our queries, are auto-updating views into the underlying data. You can think of them as type-safe cursors and they have some of the same trade-offs, but unlike Cursors we don't copy data into a CursorWindow so there is no pagination effect. You can access the entire object graph without being worried about reaching a CursorWindow limit.
Most ORM's copy all data into Java heap memory. This can be a potentially very costly operation both time- and memory-wise + you might have a lot of additional data you don't even need. The upside to this is that you do it once, then it is really fast to access the data.
Realm on the other hand, only load the data you actually need. This includes individual fields as well. So if you have a ListView showing 1 field from 10 items we will only load those 10 fields just like an CursorAdapter. It has to load that data from native memory though which is more expensive than reading it from the Java heap.
So to answer you question. Yes, Realm works perfectly fine with ListView.
回答2:
If you want to know what is faster, try it ;). Probably Realm will be faster, because their propietary system that is more optimized than SQLite, even fine-tuned. But that's my supposition
No, it mustn't have impact in performance, because we are supposing that Realm is smart enough to fetch the real data when appropiate. When using another ORM, it's the same thing, data is fetched when a pagination occurs.
来源:https://stackoverflow.com/questions/35219165/realm-lazy-queries-are-they-faster-than-ormlite