Reprinted please indicate the source: http://blog.csdn.net/droyon/article/details/35558697
When deleting or updating, you need to clear the buffer and reload the data.
1. invalidatecache () // obtain the current user's settingscache and remove all. Public settingscache cachefortable (final int callinguser, string tablename) {If (table_system.equals (tablename) {return getorconstructcache (callinguser, ssystemcaches);} If (tablename )) {return getorconstructcache (callinguser, ssecurecaches);} If (table_global.equals (tablename) {return sglobalcache;} return NULL ;}
Ii. bulkinsertpublic int bulkinsert (URI Uri, contentvalues [] values) {final int callinguser = userhandle. getcallinguserid (); // get the requested user idsettingscache cache = cachefortable (callinguser, argS. table); // get 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]);} Get settingscache (getorconstructcache ----, getorestablishdatabase ----, establishdbtracking ---, startasynccachepopulation (userhandle );) 1. If (table_system.equals (tablename) {return getorconstructcache (callinguser, ssystemcaches);} If (Response (tablename) {return getorconstructcache (callinguser, ssecurecaches );} if (table_global.equals (tabl Ename) {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 illegalargumente Xception ("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. restorecallin Gidentity (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);} Get databasehelper. Databasehelper DBH = getorestablishdatabase (table_global.equals (ARGs. Table )? Userhandle. user_owner: callinguser); insert it to the database and update the data to 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]);} sends a notification. Sendnotify (Uri, callinguser );
Iii. insert
@ Override
Public URI insert (uri url, contentvalues initialvalues ){
Return insertforuser (URL, initialvalues, userhandle. getcallinguserid ());
}
Insetforuser
1. Convert the table data 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. Get settingscache.
Settingscache cache = cachefortable (desireduserhandle, argS. Table );
3. If (settingscache. isredundantsetvalue (cache, name, value )){
Return URI. withappendedpath (URL, name );
}
4. Obtain databasehelper.
Databasehelper DBH = getorestablishdatabase (desireduserhandle );
5. Insert inset.
Final long rowid = dB. insert (ARGs. Table, null, initialvalues );
Mutationcount. decrementandget ();
If (rowid <= 0) return NULL;
6. settingscache. populate (cache, initialvalues); // before we recommend y
7. Notify
Sendnotify (URL, desireduserhandle );
Iv. Delete
- Get databasehelper
Databasehelper DBH = getorestablishdatabase (callinguser );
- Delete,
Int COUNT = dB. Delete (ARGs. Table, argS. Where, argS. ARGs );
- Invalidatecache deletes the settingscache element and sends a notification indicating the change (invalidatecache ---).
Invalidatecache (callinguser, argS. Table); // before we recommend y
Sendnotify (URL, callinguser );
Refill the settingscache buffer. (Startasynccachepopulation (callinguser); --- "new cacheprefetchthread (userhandle). Start ();)
Startasynccachepopulation (callinguser );
V. Update
- Get databasehelper
Databasehelper DBH = getorestablishdatabase (callinguser );
- Execute update
Int COUNT = dB. Update (ARGs. Table, initialvalues, argS. Where, argS. ARGs );
- Invalidatecache deletes the settingscache element and sends an update notification.
Invalidatecache (callinguser, argS. Table); // before we recommend y
Sendnotify (URL, callinguser );
Refill the settingscache buffer.
Startasynccachepopulation (callinguser );
6. query --- queryforuser
- Build query Conditions
Sqlitequerybuilder QB = new sqlitequerybuilder ();
QB. settables (ARGs. Table );
- Enable Query
Cursor ret = QB. Query (dB, select, argS. Where, argS. ARGs, null, null, sort );
- Set listening and notification.
Abstractcursor c = (abstractcursor) ret;
C. setnotificationuri (getcontext (). getcontentresolver (), URL, foruser );