Caching query result




I have a Live Query, which data I have to use several time in my app (so I need to cache it). I see that QueryEnumerator is not thread-safe and moreover I have to call reset() method to read it again. This obliges me to have a copy of QueryEnumerator with type ArrayList or something like that. What I’m concerned is that if I have a lot of data, this will cause huge memory consumption. I don’t know exact structure of Couchbase Lite, does it cache anything or always reads from file. So maybe anybody have some ideas how to behave in this case? I need fast access to a data returned from Live Query, but I don’t want to have superfluous memory consumption.

I was thinking about caching only docs ids from queries results and then access to database with those ids. But I’m not sure about fastness of data access in this case.


LiveQuery already caches all the results in memory as an array; so that when it re-runs the query it can compare the new results to the old and see whether they changed.

Memory usage will depend on the size of the keys and values the map function emits, since each QueryRow basically stores the key and value and document ID. (So if all you need from the query is to get the document, emit a null value to save space.)