Android app freezes with 100K objects dataset
See original GitHub issueGoal
I’m thinking of building an Android app using Realm.
Over time, its dataset could grow to become huge. So, to make sure I could work with Realm, I have built a simple test project with it.
My goal is to have multiple feeds to filter and group items.
Code (Kotlin)
Here are my test objects:
open class Feed(
@PrimaryKey
open var id: Long = 0,
open var name: String? = "feed$id"
) : RealmObject()
open class Item(
@PrimaryKey
open var id: Long = 0,
open var name: String = "item$id",
open var feed: Feed? = null
) : RealmObject()
I have a RecyclerView.Adapter
that receives a list of items RealmResults<Item>
object. Here’s the query:
fun Realm.getItems(feedId: Long): RealmResults<Item> =
where(Item::class.java)
.equalTo("feed.id", feedId)
.findAllSorted("id", Sort.DESCENDING)
Test
My test project dataset:
- 1
Feed
- 100K+
Item
objects
Issue
Whenever a new Item
is created, the UI freezes for 3 to 5 seconds.
If I remove the sorting on the query, the freeze is not so long, but it still breaks. Also, my dataset will be a lot more complex and 100K is just a reference number.
Skipped 159 frames! The application may be doing too much work on its main thread.
Window 'Window{2850f572 u0 io.test/io.test.activity.MainActivity}' spent 5048.6ms processing the last input event: MotionEvent(…)
Debug
With some method profiling, I noticed that io.realm.internal.TableView.nativeSyncIfNeeded()
is the culprit.
It is too slow, and it runs in the UI thread.
Question
Is there any optimization I could do to handle a dataset of this size? Thanks.
Version of Realm
Realm version: 1.2.0
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Hi @jpsim suggestion, have you tried async queries it call https://github.com/realm/realm-java/blob/master/realm/realm-library/src/main/java/io/realm/RealmResults.java#L909 on completion that will not invoke
nativeSyncIfNeeded
Cheers@jpmcosta if there is one thing you could try, it’s
RealmRecycleViewAdapter
from https://github.com/realm/realm-android-adapters andfindAllAsync()
?