Azure mobile service asynctask

守給你的承諾、 提交于 2019-12-11 20:09:34

问题


I got some trouble using the asynctask to query in my cloud database.

Due the response delay to query I cant get the result correctly. Getting NULL.

MainActivity.java

   @Override
protected void onCreate(Bundle savedInstanceState) {
    this.mBox = new Box();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.novomenu_layout);
    InicializaAzure(); // init connection to azure mobile service


    this.mPalletDao = new PalletDAO(this);
    this.mBoxDao = new BoxDAO(this);

    mBox = mBoxDao.AzureGetBoxById(1); // query the cloud database
}

BoxDAO.java

  public Box AzureGetBoxById(final long id){
    final Box[] box = new Box[1];
    final boolean[] flag = {false};



        new AsyncTask<Void, Void, Void>() {


            private ProgressDialog pDialog;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(mContext);
                pDialog.setMessage("Just a moment...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {

                    final MobileServiceList<Box> result = mBoxTable.where().field("id").eq(id).execute().get();
                    Box mBox = result.get(0);
                    box[0] = mBox;

                } catch (Exception exception) {
                    //createAndShowDialog(exception, "Error");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                pDialog.dismiss();
                flag[0] = true;
            }


        }.execute();




    return box[0];
    //return null;
}

I am getting always NULL until the asynctask has finished. but I need the result in the same time. How can I solve that? I've searched about asynctask but I didnt find anything like this.

Thank you.


回答1:


Your code is correct, and it works fine. However, if you want to get the result to show in the same time of UI displayed, you can not solve it easily by using the asynctask.

Per my experience, there are two ways can help solve that.

  1. Remove the asynctask code and use the sync method to get data, but it will cause UI hang so that it not be recommended.

  2. Use MobileServiceSyncTable to enable offline sync to solve it.

There is a sample doc https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/ to help adding offline data sync into your app.

You alse can watch some vedio to learn it, please move to http://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri and http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/.



来源:https://stackoverflow.com/questions/33622042/azure-mobile-service-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!