Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add batch with db transaction #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ProviGen/src/com/tjeannin/provigen/ProviGenProvider.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.tjeannin.provigen;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
Expand Down Expand Up @@ -308,4 +313,28 @@ private static String[] appendToStringArray(String[] array, String element) {
}
}

/**
* Apply a batch of uri's within a database transaction
* @param operations
* @return
*/
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
SQLiteDatabase db = openHelper.getWritableDatabase();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In ProviGen 2.0.0
SQLiteDatabase db = openHelper(getContext()).getWritableDatabase();

db.beginTransaction();
try {
int numOperations = operations.size();
ContentProviderResult[] results = new ContentProviderResult[numOperations];
for (int i = 0; i < numOperations; i++) {
results[i] = operations.get(i).apply(this, results, i);
//db.yieldIfContendedSafely();
}
db.setTransactionSuccessful();
return results;
} finally {
db.endTransaction();
}
}

}