네, 데이터베이스에 데이터를 저장할 때마다 수동으로 데이터베이스 변경 사항을 알릴 수있는 사용자 정의 로더를 작성할 수 있습니다.이 목적을 위해 브로드 캐스트 수신기, 녹색 로봇 이벤트 버스 등을 사용할 수 있습니다.
이벤트 버스에서 알림을받을 때마다 데이터를로드하는 사용자 정의 메시지 로더 클래스입니다. 모바일 서비스 클래스를 사용 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);
}
}
모든 데이터베이스 관련 서비스를 제공합니다.
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
나는 그것이 아니, greenDAO을 사용하는 사용자 지정 로더를 쓰고 자 할 생각? – focuser
저는 GreenDao가 Observer Pattern을 실제로 지원해야한다고 생각합니다. 심지어 Observer Pattern이 다른 방법을 사용한다고해도 말입니다. 콘텐츠 제공 업체를 사용하게 만드는 것은 바로 그 것입니다. –