java - Azure 移动服务异步任务

标签 java azure azure-mobile-services

我在使用 asynctask 在我的云数据库中查询时遇到了一些问题。

由于查询响应延迟,我无法正确获得结果。获取 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;
}

在异步任务完成之前,我总是得到 NULL。但我需要同时得到结果。 我该如何解决这个问题?我搜索过 asynctask 但没有找到类似的内容。

谢谢。

最佳答案

您的代码是正确的,并且工作正常。但是,如果你想让结果在UI显示的同时显示,使用asynctask就无法轻松解决。

根据我的经验,有两种方法可以帮助解决这个问题。

  1. 去掉asynctask代码,使用sync方法获取数据,但会导致UI挂起,不推荐。

  2. 使用MobileServiceSyncTable启用离线同步即可解决。

有一个示例文档 https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/帮助将离线数据同步添加到您的应用中。

您也可以观看视频来学习,请移步http://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayerihttp://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/ .

关于java - Azure 移动服务异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33622042/

相关文章:

java - Java中内置的比较排序

java - 如何使用 Java 从 SOAP 响应中检索元素值?

azure - 使用 AZCopy (v10) 的同步功能是否算作每个 Blob 对目标的 Blob 访问?

azure - Azure 存储文件共享上已存在指定的共享

azure - 移动应用 Azure、数据注释

ios - 使用 Swift 2 和 WindowsAzureMobileService.framework 在 iOS 客户端上实现 Azure 移动应用程序身份验证(使用 Twitter)(客户端模式)

java - Android Screencast 失败(请帮助新的 Android Dev)

java - 如何在 java 中使用 Smack XMPP 库处理 TLS 证书

azure - API 应用程序和 Web 应用程序有什么区别?

azure - 如何将日志从 azure 移动服务流式传输到控制台