How to use LoaderManager with Green-DAO

I am using GreenDAO in my current application and want to have a LoaderManager with a database connection to control changes and updates to the database on the fly.

In the documentation for Android, I saw that it is not recommended to use ContentProvider when your application has only the internal SQLite database (this is what I have), but I really want to implement the Observer Template in order to change the real-time user interface in accordance with updates to the database.

I noticed that in order to use LoaderManager I need to provide a URI for CursorLoader.

My question is: is there any sample code anywhere using this?

How can I create a LoaderManager for Green-DAO?

+4
source share
2 answers

You are not using ContentProvider and Loaders with greenDAO. At this time, these technologies do not overlap.

+1
source

Yes, you can write a custom bootloader in which you must manually report database changes when saving data to the database. For this purpose, you can use broadcast receivers, a green robot event bus, etc. See code below

A custom message loader class to load data whenever it receives notification via eventbus. MessageListLoader.java

 public class MessageListLoader extends AsyncTaskLoader<List<Message>> { private List<Message> mMessages; private long mGroupId; private Context mContext; public MessageListLoader(Context context, long groupId) { super(context); mGroupId = groupId; } private IMobileService getMobileService() { return MobileServiceImpl.getInstance(mContext); } @Override public List<Message> loadInBackground() { return getMobileService().getMessagesByGroupId(mGroupId); } @Override public void deliverResult(List<Message> newMessageList) { if (isReset()) { mMessages = null; return; } List<Message> oldMessageList = mMessages; mMessages = newMessageList; if (isStarted()) { super.deliverResult(newMessageList); } // Invalidate the old data as we don't need it any more. if (oldMessageList != null && oldMessageList != newMessageList) { oldMessageList = null; } } /** * The OnEvent method will called when new message is added to database. * * @param event */ @Subscribe public void onEvent(NewMessageEvent event) { // reload data from data base forceLoad(); } @Override protected void onStartLoading() { if (mMessages != null) { // If we currently have a result available, deliver it // immediately. deliverResult(mMessages); } if (!EventBus.getDefault().isRegistered(this)) { EventBus.getDefault().register(this); } } @Override protected void onReset() { mMessages = null; EventBus.getDefault().unregister(this); } } 

The mobile service class is used, which provides all the services associated with the database.

MobileServiceImpl.java

 public class MobileServiceImpl implements IMobileService { private static final String TAG = "MobileServiceImpl"; private static final String DATABASE_NAME = "demo.db"; private static IMobileService instance = null; private DaoSession mDaoSession; private MobileServiceImpl(Context context) { DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DATABASE_NAME, null); SQLiteDatabase db = helper.getWritableDatabase(); DaoMaster daoMaster = new DaoMaster(db); mDaoSession = daoMaster.newSession(); } public static IMobileService getInstance(Context context) { if (instance == null) { instance = new MobileServiceImpl(context); } return instance; } private MessageDao getMessageDao() { return mDaoSession.getMessageDao(); } /** * The saveMessage() method is used to save given message into database. * * @param message Specifies the message object to be saved. * @param notifyUi Specifies the boolean flag to notify the change in database to ui. * @return Saved message id. */ @Override public long saveMessage(Message message, boolean notifyUi) { long id = getMessageDao().insert(message); if (notifyUi) EventBus.getDefault().post(new NewMessageEvent(id)); return id; } @Override public List<Message> getMessagesByGroupId(long groupId) { return getMessageDao() .queryBuilder() .where(MessageDao.Properties.GroupId.eq(groupId)) .orderDesc(MessageDao.Properties.Id).list(); } @Override public Message getMessageById(long messageId) { return getMessageDao().load(messageId); } } 

Download sample project here

0
source

Source: https://habr.com/ru/post/1446735/


All Articles