SettingsProvider之增刪改查

來源:互聯網
上載者:User

標籤:settingsprovider   settingsprovider之增刪改   

轉載請註明出處:http://blog.csdn.net/droyon/article/details/35558697

當delete或者update時,需要清空緩衝區並重新載入資料。

1、invalidateCache()//得到目前使用者的SettingsCache,remove所有。public SettingsCache cacheForTable(final int callingUser, String tableName) {        if (TABLE_SYSTEM.equals(tableName)) {            return getOrConstructCache(callingUser, sSystemCaches);        }        if (TABLE_SECURE.equals(tableName)) {            return getOrConstructCache(callingUser, sSecureCaches);        }        if (TABLE_GLOBAL.equals(tableName)) {            return sGlobalCache;        }        return null;    }

二、bulkInsertpublic int bulkInsert(Uri uri, ContentValues[] values) {final int callingUser = UserHandle.getCallingUserId();//得到請求使用者idSettingsCache cache = cacheForTable(callingUser, args.table);//得到SettingsCache int numValues = values.length;            for (int i = 0; i < numValues; i++) {                if (db.insert(args.table, null, values[i]) < 0) return 0;                SettingsCache.populate(cache, values[i]);                if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);            }    得到SettingsCache(getOrConstructCache  ----》 getOrEstablishDatabase  ----》  establishDbTracking   ---》 startAsyncCachePopulation(userHandle);)    1、if (TABLE_SYSTEM.equals(tableName)) {                return getOrConstructCache(callingUser, sSystemCaches);            }            if (TABLE_SECURE.equals(tableName)) {                return getOrConstructCache(callingUser, sSecureCaches);            }            if (TABLE_GLOBAL.equals(tableName)) {                return sGlobalCache;            }    2、private SettingsCache getOrConstructCache(int callingUser, SparseArray<SettingsCache> which) {            getOrEstablishDatabase(callingUser); // ignore return value; we don't need it            return which.get(callingUser);        }    3、private DatabaseHelper getOrEstablishDatabase(int callingUser) {            if (callingUser >= Process.SYSTEM_UID) {                if (USER_CHECK_THROWS) {                    throw new IllegalArgumentException("Uid rather than user handle: " + callingUser);                } else {                    Slog.wtf(TAG, "establish db for uid rather than user: " + callingUser);                }            }            long oldId = Binder.clearCallingIdentity();            try {                DatabaseHelper dbHelper = mOpenHelpers.get(callingUser);                if (null == dbHelper) {                    establishDbTracking(callingUser);                    dbHelper = mOpenHelpers.get(callingUser);                }                return dbHelper;            } finally {                Binder.restoreCallingIdentity(oldId);            }        }    4、private void startAsyncCachePopulation(int userHandle) {            new CachePrefetchThread(userHandle).start();        }    class CachePrefetchThread extends Thread {            private int mUserHandle;            CachePrefetchThread(int userHandle) {                super("populate-settings-caches");                mUserHandle = userHandle;            }            @Override            public void run() {                fullyPopulateCaches(mUserHandle);            }        }    5、fullyPopulateCaches    private void fullyPopulateCaches(final int userHandle) {            DatabaseHelper dbHelper = mOpenHelpers.get(userHandle);            // Only populate the globals cache once, for the owning user            if (userHandle == UserHandle.USER_OWNER) {                fullyPopulateCache(dbHelper, TABLE_GLOBAL, sGlobalCache);            }            fullyPopulateCache(dbHelper, TABLE_SECURE, sSecureCaches.get(userHandle));            fullyPopulateCache(dbHelper, TABLE_SYSTEM, sSystemCaches.get(userHandle));        }    得到DatabaseHelper。    DatabaseHelper dbH = getOrEstablishDatabase(                    TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);    插入到資料庫,更新資料到SettingsCache。    for (int i = 0; i < numValues; i++) {                    if (db.insert(args.table, null, values[i]) < 0) return 0;                    SettingsCache.populate(cache, values[i]);                    if (LOCAL_LOGV) Log.v(TAG, args.table + " <- " + values[i]);                }發送通知。sendNotify(uri, callingUser);

三、Insert
@Override
    public Uri insert(Uri url, ContentValues initialValues) {
        return insertForUser(url, initialValues, UserHandle.getCallingUserId());
    }
insetForUser
1、轉換table資料表。
if (name != null) {
            if (sSecureGlobalKeys.contains(name) || sSystemGlobalKeys.contains(name)) {
                if (!TABLE_GLOBAL.equals(args.table)) {
                    if (LOCAL_LOGV) Slog.i(TAG, "Rewrite of insert() of now-global key " + name);
                }
                args.table = TABLE_GLOBAL;  // next condition will rewrite the user handle
            }
        }
2、得到SettingsCache
SettingsCache cache = cacheForTable(desiredUserHandle, args.table);
3、if (SettingsCache.isRedundantSetValue(cache, name, value)) {
            return Uri.withAppendedPath(url, name);
        }
4、得到DatabaseHelper
DatabaseHelper dbH = getOrEstablishDatabase(desiredUserHandle);
5、inset插入。
final long rowId = db.insert(args.table, null, initialValues);
        mutationCount.decrementAndGet();
        if (rowId <= 0) return null;
6、SettingsCache.populate(cache, initialValues);  // before we notify
7、notify
sendNotify(url, desiredUserHandle);


四、delete

  • 得到DatabaseHelper
    DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
  • delete,
    int count = db.delete(args.table, args.where, args.args);
  • invalidateCache刪除SettingsCache元素,並發送通知告知變化(invalidateCache -- -》 )
    invalidateCache(callingUser, args.table);  // before we notify
                sendNotify(url, callingUser);

重新填充SettingsCache緩衝區。(startAsyncCachePopulation(callingUser); ---》 new CachePrefetchThread(userHandle).start();)
startAsyncCachePopulation(callingUser);
五、update

  • 得到DatabaseHelper
    DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
  • 執行update
    int count = db.update(args.table, initialValues, args.where, args.args);
  • invalidateCache刪除SettingsCache元素,發通知更新。
    invalidateCache(callingUser, args.table);  // before we notify
                sendNotify(url, callingUser);

重新填充SettingsCache緩衝區。
startAsyncCachePopulation(callingUser);
六、query ---》 queryForUser

  • 構建查詢條件

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
            qb.setTables(args.table);
  • 開啟查詢
    Cursor ret = qb.query(db, select, args.where, args.args, null, null, sort);
  • 設定監聽並通知。
    AbstractCursor c = (AbstractCursor) ret;
                c.setNotificationUri(getContext().getContentResolver(), url, forUser);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.