質問

I'm using GreenDAO in my current app, and want to have a LoaderManager with a connection to the DB in order to monitor changes and updates on the DB on the fly.

I've seen in the Android documentation that it's not recommended to use a ContentProvider when your app has only an internal SQLite DB (which is what I have) however, I really want to implement the Observer Pattern in order to change the UI in real time according to the updates in the DB.

I've noticed that in order to use the LoaderManager, I need to give a URI for the CursorLoader.

My question is, is there some sample code anywhere using this?

how can I create a LoaderManager for a Green-DAO?

役に立ちましたか?

解決

You don't use ContentProvider and Loaders with greenDAO. At this time, those technologies do not intersect.

他のヒント

Yes, You can write a custom loader where you have to notify about the database changes manually whenever you save data in database.For that purpose you can use Broadcast Receivers,Green robot Event bus etc.See the code below

Custom message loader class to load data whenever it get notified by 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 provide all database related services.

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 from Here

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top