簡體   English   中英

如何等待直到異步任務中另一個類的方法在Android中完成

[英]How to wait until method from another class in asynctask finishes in android

我是AsyncTask的新手,但遇到了問題。 我有AsyncTask,它通過從另一個類調用函數將信息保存在DB中。 問題在於onPostExecute方法在我的函數完成之前正在調用; 這是我的代碼:

class checkNewReviews2 extends AsyncTask<List<ReviewList.ReviewItem>, Void, Void>{


    @Override
    protected Void doInBackground(List<ReviewList.ReviewItem>... reviewList) {

        int size = reviewList[0].size();
        if(size>0) {

            for (int i = 0; i < size; i++) {
                ReviewList.ReviewItem r = reviewList[0].get(i);
                ContentValues c = new ContentValues();
                c.put(LentaClass.LentaListEntry.FILM_ID, r.getFilm_id());
                c.put(LentaClass.LentaListEntry.USER_ID, r.getUser_id());
                c.put(LentaClass.LentaListEntry.REVIEW_TEXT, r.getReview_text());
                c.put(LentaClass.LentaListEntry.CREATED_AT, r.getCreated_at());
                c.put(LentaClass.LentaListEntry.REVIEW_TYPE, r.getReview_type());
                c.put(LentaClass.LentaListEntry.VIEWS, r.getViews());
                sqLiteDatabase.insert(LentaClass.LentaListEntry.TABLE_NAME, null, c);

                c.clear();
            }
            FilmHandler filmHandler = new FilmHandler(getContext());
            filmHandler.HandleFilms(reviewList[0]);
            UserHandler userHandler = new UserHandler(getContext());
            userHandler.HandleUsers(reviewList[0]);


        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        initiateRecyclerView();
    }
}

我已經嘗試將所有調用放入onPreExecute,但是結果仍然相同。 還要注意,成功處理了第一段sql代碼(循環),而不是將其作為Film和User處理程序。 在AsyncTask完全執行后,我該如何調用initiateRecyclerView?

您的doInBackgound()方法需要“等待”您的FilmHandlerUserHandler類完成。

我的猜測是您的類在后台線程上工作,因此當您使用它們時,代碼將立即繼續執行return null語句-並完成后台工作,從而導致onPostExecute()

更改您的類實現以在調用線程上工作而不創建自己的類。

您是否也可以在doinbackground參數中添加上下文。可能是getcontext返回null / Application Context

onPostExecute將僅在doInBackground完成后執行。 如果FilmHandler和Userhandler是在單獨的線程上實現的,則可能導致此問題。 因此,為避免這種情況,請勿使用其他線程在DB中保存數據(或)執行其他某種操作,而應在doInBackground本身中完成整個保存數據。

另外,還有另一種方法可以存檔功能。 您需要為filmHandler或userHandler函數添加回調,並在doinBackground中檢查更新,然后可以基於該更新將結果傳遞給PostExcecute()

class checkNewReviews2 extends AsyncTask<List<ReviewList.ReviewItem>, Void, Boolean>{


@Override
protected Void doInBackground(List<ReviewList.ReviewItem>... reviewList) {

    int size = reviewList[0].size();
    if(size>0) {

        for (int i = 0; i < size; i++) {
            ReviewList.ReviewItem r = reviewList[0].get(i);
            ContentValues c = new ContentValues();
            c.put(LentaClass.LentaListEntry.FILM_ID, r.getFilm_id());
            c.put(LentaClass.LentaListEntry.USER_ID, r.getUser_id());
            c.put(LentaClass.LentaListEntry.REVIEW_TEXT, r.getReview_text());
            c.put(LentaClass.LentaListEntry.CREATED_AT, r.getCreated_at());
            c.put(LentaClass.LentaListEntry.REVIEW_TYPE, r.getReview_type());
            c.put(LentaClass.LentaListEntry.VIEWS, r.getViews());
            sqLiteDatabase.insert(LentaClass.LentaListEntry.TABLE_NAME, null, c);

            c.clear();
        }


        FilmHandler filmHandler = new FilmHandler(YourActivity.this);
       boolean filmHandlerState =   filmHandler.HandleFilms(reviewList[0]);
        UserHandler userHandler = new UserHandler(YourActivity.this);
      boolean userHandlerState =   userHandler.HandleUsers(reviewList[0]);



        if(filmHandlerState && userHandlerState )
        {
        return true;
        } 
       else
       {
       return false;
       }
    }
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);

      if(result)
     {
       initiateRecyclerView();
     }
     else
     {
      // any one of the filmHandler or userHandler function has failed. so do your handling here 

}

}

@ user3413619指出的另一點可能是getContext()返回null,因此,請嘗試將其設置為YourActivity.this

使doInBackground方法返回boolean類型,並將結果傳遞給postExecute boolean,然后檢查result是否為true即可執行您要調用的方法。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM