简体   繁体   中英

Storing data and displaying in Recyclerview using Realm

I recently started learning about storing data in Android using SQLite. I find the SQLite commands hard to learn so I started looking for alternatives. I came across Realm and I am giving it a try. But, I am having some difficulty understanding its usage.

I have 4 classes in my app.

MainActivity - Has the RealmObject

Names - The base class which extends RealmObject. It has all the setters and getters

BackgroundService - An IntentService which gets data from web and should store it in the RealmObject

RecyclerViewAdapter - should be able to get data from the RealmObject in MainActivity and display it to user

I have read in many places that realm cannot be accessed from different thread. Can I access a single RealmObject from different classes? How do I convert RealmObject to a List so I can use it in RecyclerView?

I have read in many places that realm cannot be accessed from different thread.

Sharing the same realm between multiple threads is no problem. You just have to make sure they each retrieve their own instances of the objects. Check out the section about threading in the realm docs: https://realm.io/docs/java/latest/#threading

How do I convert RealmObject to a List so I can use it in RecyclerView?

That should not be needed. Have you seen this nice RecyclerView component: https://github.com/thorbenprimke/realm-recyclerview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM