我的應用程序包括以下活動決定何時讀取網絡數據VS時從緩存
活動A的閱讀 - >連接到Web服務器,並獲得用戶的列表。它還會在數據庫中緩存該列表,以便提供離線訪問以及不向服務器發出重複的http請求。
單擊活動A中的項目會顯示活動B,其中包含與用戶有關的信息。它也被緩存在數據庫中。
目前,我決定要請求到服務器獲取用戶列表只有當沒有數據存在於數據庫中,或者如果活動A的onCreate方法被調用。下面是它
@Override
protected void onResume() {
super.onResume();
dbHelper = new DatabaseHelper(this);
int numOfUsers = dbHelper.numOfUsers(); //Retrieve the count of users
if (numOfUsers <= 0 || isOnCreateCalled) {
// retrieve the list of users from the server
// Update the database with the new values
} else {
// retrieve the list of users from the database
}
}
的代碼片段類似的方法被用於緩存存在於活動B.與用戶相關的數據
我想知道有沒有什麼比這個簡單的方法更好的戰略決定何時從網絡讀取數據並使緩存的數據庫數據無效,與何時從緩存中讀取。
在此先感謝。