【问题标题】:Is it ever safe to call Volley from a thread other than the main UI thread?从主 UI 线程以外的线程调用 Volley 是否安全?
【发布时间】:2013-12-09 17:05:59
【问题描述】:

Volley ImageLoader 强制要求应该从主 UI 线程发起请求:

public ImageContainer get(String requestUrl, ImageListener imageListener,
        int maxWidth, int maxHeight) {
    // only fulfill requests that were initiated from the main thread.
    throwIfNotOnMainThread();

我认为这是为了保证响应回调发生在主 UI 线程上,大概是因为假设调用者希望在回调中更新 UI。

但是,我想在处理 GCM 通知时使用 Volley,它只需要发送 PendingIntent,而不是直接更新 UI。我想知道在这种特殊情况下删除throwIfNotOnMainThread() 是否安全。我怀疑这是否安全,但我想知道是否有人可以给出一个充分的理由?

(仅供参考,看起来RequestQueue 已同步访问mCurrentRequests,因此 Volley 内部的请求队列似乎存在内部锁定。)

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    我不建议删除该保护,因为在ImageContainer 的一般用例中确实会更改 UI。

    IMO 更好的方法是直接将 RequestQueue 与自定义请求一起使用并以您想要的方式处理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多