【问题标题】:Asynchronous volley calls in android SyncAdapterandroid SyncAdapter 中的异步齐射调用
【发布时间】:2016-05-02 18:13:12
【问题描述】:

我使用 volley 库实现了 SyncAdapter。它正在工作,但后来我意识到我正在从 onPerformSync 方法调用异步(齐射请求)代码。

  • Q1:onPerformSync 可以并行执行多次吗? (对于一个用户/一个权限)。我需要编写内部代码并发安全吗?使用锁?同步? SyncAdapter 不是自己同步的,所以任何内部同步都没用?
  • Q2:onPerformSync 线程安全吗,哪个线程?在我看来,所有 onPerformSync 调用都是由同一个线程引用完成的。这是否意味着 SyncAdapter 实际上被系统多次重用?
  • Q3:在同步代码完成之前结束 onPerformSync 是否安全? (凌空调用可能需要更长的时间,而不仅仅是创建凌空请求、运行它并完成)
    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient contentProviderClient, SyncResult syncResult) {
        JsonObjectRequest jsObjRequest = new JsonObjectRequest
                (Request.Method.GET, url, null, new Response.Listener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        // time consuming code
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // TODO Auto-generated method stub
                    }
                });
        // Access the RequestQueue through your singleton class.
        MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
        // onPerformSync end reached before volley request processing ends
    }

【问题讨论】:

    标签: android multithreading asynchronous concurrency android-syncadapter


    【解决方案1】:

    您可以使用CountDownLatch ,但我读到这不是在同步适配器中实现它对我有用的好方法

        
        private CountDownLatch doneSignal = new CountDownLatch(1);
        
        @Override
        public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient contentProviderClient, SyncResult syncResult) {
            JsonObjectRequest jsObjRequest = new JsonObjectRequest
                    (Request.Method.GET, url, null, new Response.Listener() {
                        @Override
                        public void onResponse(JSONObject response) {
                            // time consuming code
                            //put this line as the last line when everything is done
                            doneSignal.countDown();
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            // TODO Auto-generated method stub
                        }
                    });
            // Access the RequestQueue through your singleton class.
            MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
            try {
                doneSignal.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // onPerformSync end reached before volley request processing ends
        }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2020-04-30
      相关资源
      最近更新 更多