Android cookie 之非同步儲存

來源:互聯網
上載者:User

android Cookie儲存到database是非同步實現的。

CookieSyncManager繼承 WebSyncManager,
是負 責cookie從記憶體到持久儲存的
類,cookie 的同步可以分為自動同步和強制同步兩種.

1. 自動同步
自動同步
是每過5分鐘同步一次,是開啟一個線程來負責ram到
database的同步,這個線程維護訊息佇列,線程啟動時發一個delay時間為 SYNC_LATER_INTERVAL(5min)的 訊息
   Message msg =
mHandler.obtainMessage(SYNC_MESSAGE);
   mHandler.sendMessageDelayed(msg, SYNC_LATER_INTERVAL);
   每次處理完這個訊息會添加另外一個delay為5min的訊息。

2.強制同步
強制同步調用sync()介面
 public void sync() {
        if (DebugFlags.WEB_SYNC_MANAGER) {
            Log.v(LOGTAG, "*** WebSyncManager sync ***");
        }
        if (mHandler == null) {
            return;
        }
        mHandler.removeMessages(SYNC_MESSAGE);
        Message msg =
mHandler.obtainMessage(SYNC_MESSAGE);
        mHandler.sendMessageDelayed(msg,
SYNC_NOW_INTERVAL);
    }
sync方法是觸發一個delay為100毫秒的訊息。

不管哪種,cookie儲存是通過另外一個線程的Handler來實現的,需要Looper到這個訊息才去做持久化操作,是
非同步實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.