Created
November 15, 2012 15:14
-
-
Save saik0/4079112 to your computer and use it in GitHub Desktop.
Sample ContentProvider that performs all operations transactionally and notifies once in batch operations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyContentProvider extends ContentProvider { | |
private final ThreadLocal<Boolean> mApplyingBatch; | |
private final ThreadLocal<Set<Uri>> mChangedUris; | |
private boolean applyingBatch() { | |
return mApplyingBatch.get() != null && mApplyingBatch.get(); | |
} | |
private Uri insert(final Uri uri, final ContentValues values, final SQLiteDatabase db) { | |
// do the uri matching and insert | |
return resultUri; | |
} | |
@Override | |
public Uri insert(final Uri uri, final ContentValues values) { | |
/* | |
* there are no observers on content that does not exist | |
* so we add the base uri to the set for batch operations | |
* for delete/update we notify with the id instead. | |
*/ | |
final SQLiteDatabase db = getDatabase(getContext()); | |
if (!applyingBatch()) { | |
mChangedUris.set(new HashSet<Uri>()); | |
db.beginTransaction(); | |
try { | |
Uri resultUri = insert(uri, values, db); | |
db.setTransactionSuccessful(); | |
mChangedUris.get().add(uri); | |
return resultUri; | |
} finally { | |
db.endTransaction(); | |
onOperationComplete(); | |
} | |
} else { | |
Uri resultUri = insert(uri, values, db); | |
mChangedUris.get().add(uri); | |
return resultUri; | |
} | |
} | |
@Override | |
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) | |
throws OperationApplicationException { | |
mChangedUris.set(new HashSet<Uri>()); | |
db.beginTransaction(); | |
try { | |
mApplyingBatch.set(true); | |
final int numOperations = operations.size(); | |
final ContentProviderResult[] results = new ContentProviderResult[numOperations]; | |
for (int i = 0; i < numOperations; i++) { | |
results[i] = operations.get(i).apply(this, results, i); | |
} | |
db.setTransactionSuccessful(); | |
return results; | |
} finally { | |
db.endTransaction(); | |
mApplyingBatch.set(false); | |
onOperationComplete(); | |
} | |
} | |
private void onOperationComplete() { | |
for (Uri uri : mChangedUris.get()) { | |
getContext().getContentResolver().notifyChange(uri, null); | |
} | |
} | |
@Override | |
public int bulkInsert(final Uri uri, final ContentValues[] values) { | |
// for performance we dont want to call this.insert, better only match Uri once here | |
final SQLiteDatabase db = getDatabase(getContext()); | |
int numberInserted = 0; | |
db.beginTransaction(); | |
try { | |
// iterate through values and do your db.insert satements | |
} finally { | |
db.endTransaction(); | |
} | |
// now just notify the base url | |
getContext().getContentResolver().notifyChange(uri, null); | |
return numberInserted; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is beautiful.. thanks ...