2

在我的聊天应用程序中,我使用 SQLBrite 包装器从本地数据库更新聊天。目前我的活动同时包含 UI 和 SQLBrite。现在我想将 SQLBrite 逻辑提取到外部类。因此,当我的活动还活着时,我需要提取的 SQLBrite 也活着。目前我有订阅者的问题。我还将它提取到外部类,并且它一直在发出查询,尽管一切都与它在活动时一样,并且它正在工作。

这是旧代码,所有内容都在同一个活动中:我该怎么做?

SqlBrite sqlBrite;
sqlBrite = SqlBrite.create(DatabaseManager.DatabaseHelper);
private final CompositeSubscription subscriptions = new CompositeSubscription();

private void saveMessage(Message message) {

        ContentValues values = new ContentValues();
        values.put(DatabaseContract.MessageEntry.COLUMN_NAME_MESSAGE_ID, message.getId());
        values.put(DatabaseContract.MessageEntry.COLUMN_NAME_SERIAL_NUMBER, message.getSerialNumber());
        values.put(DatabaseContract.MessageEntry.COLUMN_NAME_TEXT, message.getText());

        sqlBrite.insert(DatabaseContract.MessageEntry.TABLE_NAME, values, SQLiteDatabase.CONFLICT_IGNORE);
    }

    private void subscribeToDbChanges() {
        String NEW_MSG_QUERY = "SELECT * FROM "
                + DatabaseContract.MessageEntry.TABLE_NAME
                + " WHERE "
                + DatabaseContract.MessageEntry.COLUMN_NAME_CREATED_AT_MILLS
                + " >= ? ORDER BY "
                + DatabaseContract.MessageEntry.COLUMN_NAME_CREATED_AT_MILLS
                + " DESC";

        String grpId = String.valueOf(groupId);
        String msgDate = String.valueOf(dividerMsgDate);

        Observable<Query> freshMessages = sqlBrite.createQuery(DatabaseContract.MessageEntry.TABLE_NAME, NEW_MSG_QUERY, grpId, msgDate);
        subscriptions.add(
                freshMessages.subscribe(query -> parseQueryData(query)));
    }

    private void parseQueryData(Query query) {
        Log.d(TAG, "subscriber called");
        List<Message> messages = new ArrayList<>();
        Cursor c = query.run();
        try {
            while (c.moveToNext()) {
                Message message = new Message();
                message.setLocalId(c.getInt(c.getColumnIndexOrThrow(DatabaseContract.MessageEntry._ID)));
                message.setId(c.getInt(c.getColumnIndexOrThrow(DatabaseContract.MessageEntry.COLUMN_NAME_MESSAGE_ID)));

                messages.add(message);
            }
        } finally {
            c.close();
        }

        updateChatList(messages, "new");
    }
4

1 回答 1

1

我不是 100% 确定我理解了您的问题,但我会使用您在上面提供的代码创建一个 SQLBrite Helper 类,并在活动的 onCreate() 中实例化它。

此外,您必须将 onResume() 和 onPause() 事件传递给您的助手类(因为您仅在活动处于活动状态时才需要它),并通过回调返回结果。

 MyActivity.java


 public class MyActivity extends Activity implements SQLHelper.ResultListener {

   SQLHelper mSQLHelper;

   public void onCreate() {
     mSQLHelper = new SQLHelper(this);
   }


   public void onResume() {
     mSQLHelper.onResume();
   }


   public void onPause() {
     mSQLHelper.onPause();
   }

   @
   Override
   public void onUpdateChatList(String _message) {
     //show it
   }
 }


 SQLHelper.java

 public class SQLHelper {

   ResultListener mListener;

   public interface ResultListener {
     public void onUpdateChatList(String _message);
   }

   public void SQLHelper(ResultListener _resultListener) {
     mListener = _resultListener;
   }


   public void onResume() {
     //Here subscribe to your query
   }


   public void onPause() {
     //here unsubscribe
   }

   private void parseQueryData(Query query) {
     //... your code
     mListener.onUpdateChatList(String _message);
   }
 }

希望有帮助。

于 2015-05-10T09:26:10.387 回答