Azure的移动服务的AsyncTask [英] Azure mobile service asynctask

查看:237
本文介绍了Azure的移动服务的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AsyncTask的在我的云数据库来查询了一些麻烦。

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

由于响应延迟查询我不能得到正确的结果。获取NULL。

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;
}

直到AsyncTask的完成我得到总是NULL。但我需要的结果的同时。
我该如何解决呢?我搜索有关的AsyncTask,但我没有发现这样的事。

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.

感谢您。

推荐答案

您code是正确的,它工作正常。不过,如果你想要得到的结果中所显示的用户界面的同时显示,你不能轻易使用AsyncTask的解决这个问题。

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. 删除AsyncTask的code和使用同步方法来获取数据,但它会造成用户界面挂起,使其不被推荐。

  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.

使用 MobileServiceSyncTable 启用脱机同步来解决它。

Use MobileServiceSyncTable to enable offline sync to solve it.

有一个样本文档<一个href=\"https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/\" rel=\"nofollow\">https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/以帮助增加离线数据同步到您的应用程序。

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.

您ALSE可以看一些录象学呢,请移动到的http://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri和<一个href=\"http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/\" rel=\"nofollow\">http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/.

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/.

这篇关于Azure的移动服务的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆