将一个方法的输出传递到同一线程中的另一个方法

时间:2018-09-30 09:18:08

标签: java android

我的Asynch任务的doinBackround方法中有两种方法。
第一种方法用于从Cloud Database检索值。 第二种方法用于在msqlite中保存值。

我的问题是当第二种方法执行时,第一种方法无法在需要时产生值,因此传递了null。请查看代码段。

    @Override
        protected Void doInBackground(Param... params) {
        try {
            List<Object> objectlist = mAsyncTaskFireDB.getobjectlist(params[0]);
         // Output of this method to be passed into 2nd method.
            Object[] listarray = new Object[objectlist.size()];
            for(Integer i=0;i<objectlist.size();i++)
                {
                 listarray[i] = objectlist.get(i);
                 }
            mCat_messageDao.insertAll(listarray);
            // 2nd method : Value of 1st method should come here however 
              getting null here

已通过Google搜索并找到正确的解决方案。我想知道什么是处理这种常见情况的最佳方法。提前感谢您的帮助。

编辑

  Asynch Class Code below
private static class insertAsyncTaskFireDB extends AsyncTask<Chat_Message,Void, Void> {

    private Firestore_dB mAsyncTaskFireDB;
    private Chat_MessageDao mCat_messageDao;
    private OnChatMsgreadFStoredB onChatMsglistner;
    insertAsyncTaskFireDB(Firestore_dB dB,Chat_MessageDao mChat_messageDao,OnChatMsgreadFStoredB calback) {
        mAsyncTaskFireDB = dB;
        mCat_messageDao = mChat_messageDao;
        onChatMsglistner = calback;
    }
    @Override
    protected Void doInBackground(final Chat_Message... params) {

        if(params[0].groupID == null)
        {
            params[0].setGroupID(mAsyncTaskFireDB.getChatMsgforSingleGrp(params[0].tempgrpID));
             }
        else {
            try {
       mAsyncTaskFireDB.get_chat_message(params[0], new OnChatMsgreadFStoredB() {
         @Override
      public List<Chat_Message> onMsgreadCompleteFstoredB(List<Chat_Message> chat_messages) {
      Chat_Message[] msgarray = new Chat_Message[chat_messages.size()];

       for(Integer i=0;i<chat_messages.size();i++)
        {
           msgarray[i] = chat_messages.get(i);
         }

          mCat_messageDao.insertAll(msgarray);
            return null;
                     }
                 });
                                }
            catch(Error e){Log.d(TAG, "Error is Asynch Class fetcing details from Sqlite"+e);}

        }
        return null;
    }
}

3 个答案:

答案 0 :(得分:1)

由于基于监听器,您似乎可以从主线程访问get_chat_message,因此在保存数据时,您只需要在主线程中执行该部分即可。请尝试以下

mAsyncTaskFireDB.get_chat_message(params[0], new OnChatMsgreadFStoredB() {
  @Override
  public List<Chat_Message> onMsgreadCompleteFstoredB(List<Chat_Message> chat_messages) {
      Chat_Message[] msgarray = new Chat_Message[chat_messages.size()];
      for(Integer i=0;i<chat_messages.size();i++)
      {
        msgarray[i] = chat_messages.get(i);
      }
      new DBTask(msgarray).execute();
      return null;
  }
});

class DBTask extends AsyncTask<Void,Void, Void> {
    Chat_Message[] msgarray;
    public DBTask(Chat_Message[] data){
      msgarray = data;
    }

    @Override
    protected Void doInBackground(Void ...voids) {
       mCat_messageDao.insertAll(msgarray);
    }
}

答案 1 :(得分:0)

您应该使用AsyncTask.get() 这将使您等到任务完成。 请注意,如果在那里使用它,它将冻结您的UI线程。

答案 2 :(得分:0)

您可以看一下CompletableFuture API。

使用runAsyncthenApplyAsync之类的方法,您可以轻松地处理需要异步运行的任务,也可以让它们一个接一个地运行,使用上一步到下一步的结果。使用exceptionally,还可以仅在上一个任务以异常结束的情况下运行指定的任务。

如果您主要使用并发性,那么它是一个很棒的API,并且非常有用。

相关问题