Java Code Examples for android.database.sqlite.SQLiteDatabase#yieldIfContendedSafely()

The following examples show how to use android.database.sqlite.SQLiteDatabase#yieldIfContendedSafely() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseContentProvider.java    From PreferencesProvider with Apache License 2.0 6 votes vote down vote up
@Override
public int bulkInsert(Uri uri, ContentValues[] values) {
    String table = uri.getLastPathSegment();
    SQLiteDatabase db = mSqLiteOpenHelper.getWritableDatabase();
    int res = 0;
    db.beginTransaction();
    try {
        for (ContentValues v : values) {
            long id = db.insert(table, null, v);
            db.yieldIfContendedSafely();
            if (id != -1) {
                res++;
            }
        }
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    String notify;
    if (res != 0 && ((notify = uri.getQueryParameter(QUERY_NOTIFY)) == null || "true".equals(notify))) {
        getContext().getContentResolver().notifyChange(uri, null);
    }

    return res;
}
 
Example 2
Source File: SQLiteContentProvider.java    From opentasks-provider with Apache License 2.0 6 votes vote down vote up
@Override
public int bulkInsert(Uri uri, ContentValues[] values)
{
	int numValues = values.length;
	boolean callerIsSyncAdapter = isCallerSyncAdapter(uri);
	SQLiteDatabase db = mOpenHelper.getWritableDatabase();
	db.beginTransaction();
	try
	{
		for (int i = 0; i < numValues; i++)
		{
			insertInTransaction(db, uri, values[i], callerIsSyncAdapter);
			db.yieldIfContendedSafely();
		}
		db.setTransactionSuccessful();
	}
	finally
	{
		db.endTransaction();
	}

	onEndTransaction(callerIsSyncAdapter);
	return numValues;
}
 
Example 3
Source File: CPOrmContentProvider.java    From CPOrm with MIT License 5 votes vote down vote up
@Override
public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] values) {

    int length = values.length;
    if (length == 0)
        return 0;

    TableDetails tableDetails = uriMatcherHelper.getTableDetails(uri);
    SQLiteDatabase db = database.getWritableDatabase();

    if (debugEnabled) {
        CPOrmLog.d("********* Bulk Insert **********");
        CPOrmLog.d("Uri: " + uri);
    }

    int count = 0;

    try {
        db.beginTransactionNonExclusive();
        String tableName = tableDetails.getTableName();
        for (int i = 0; i < length; i++) {

            db.insertOrThrow(tableName, null, values[i]);
            count++;

            if (count % 100 == 0)
                db.yieldIfContendedSafely();
        }

        db.setTransactionSuccessful();

        notifyChanges(uri.buildUpon().appendQueryParameter(PARAMETER_CHANGE_TYPE, CPOrm.ChangeType.INSERT.toString()).build(), tableDetails);
    } finally {
        db.endTransaction();
    }

    return count;
}
 
Example 4
Source File: LocalLogContentProvider.java    From nRF-Logger-API with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected boolean yield(LogTransaction transaction) {
	// Now proceed with the DB yield.
	SQLiteDatabase db = transaction.getDbForTag(DB_TAG);
	return db != null && db.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY);
}
 
Example 5
Source File: SQLiteContentProvider.java    From opentasks-provider with Apache License 2.0 4 votes vote down vote up
@Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations) throws OperationApplicationException
{
	int ypCount = 0;
	int opCount = 0;
	boolean callerIsSyncAdapter = false;
	SQLiteDatabase db = mOpenHelper.getWritableDatabase();
	db.beginTransaction();
	try
	{
		mApplyingBatch.set(true);
		final int numOperations = operations.size();
		final ContentProviderResult[] results = new ContentProviderResult[numOperations];
		for (int i = 0; i < numOperations; i++)
		{
			if (++opCount >= MAX_OPERATIONS_PER_YIELD_POINT)
			{
				throw new OperationApplicationException("Too many content provider operations between yield points. "
					+ "The maximum number of operations per yield point is " + MAX_OPERATIONS_PER_YIELD_POINT, ypCount);
			}
			final ContentProviderOperation operation = operations.get(i);
			if (!callerIsSyncAdapter && isCallerSyncAdapter(operation.getUri()))
			{
				callerIsSyncAdapter = true;
			}
			if (i > 0 && operation.isYieldAllowed())
			{
				opCount = 0;
				if (db.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY))
				{
					ypCount++;
				}
			}
			results[i] = operation.apply(this, results, i);
		}
		db.setTransactionSuccessful();
		return results;
	}
	finally
	{
		mApplyingBatch.set(false);
		db.endTransaction();
		onEndTransaction(callerIsSyncAdapter);
	}
}