示例#1
0
  @Override
  public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
      throws OperationApplicationException {
    Log.d(TAG, "applyBatch()");
    ContentProviderResult[] result = new ContentProviderResult[operations.size()];
    int i = 0;
    // Opens the database object in "write" mode.
    SQLiteDatabase db = database.getWritableDatabase();
    // Begin a transaction
    db.beginTransaction();
    try {
      for (ContentProviderOperation operation : operations) {
        // Chain the result for back references
        result[i++] = operation.apply(this, result, i);
      }

      db.setTransactionSuccessful();
    } catch (OperationApplicationException e) {
      Log.d(TAG, "batch failed: " + e.getLocalizedMessage());
    } finally {
      db.endTransaction();
    }
    Log.d(TAG, "applyBatch() result[0] " + result[0]);
    return result;
  }
 @Override
 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
     throws OperationApplicationException {
   HashSet<Uri> urisToNotify = new HashSet<Uri>(operations.size());
   for (ContentProviderOperation operation : operations) {
     urisToNotify.add(operation.getUri());
   }
   SQLiteDatabase db = mSqLiteOpenHelper.getWritableDatabase();
   db.beginTransaction();
   try {
     int numOperations = operations.size();
     ContentProviderResult[] results = new ContentProviderResult[numOperations];
     int i = 0;
     for (ContentProviderOperation operation : operations) {
       results[i] = operation.apply(this, results, i);
       if (operation.isYieldAllowed()) {
         db.yieldIfContendedSafely();
       }
       i++;
     }
     db.setTransactionSuccessful();
     for (Uri uri : urisToNotify) {
       getContext().getContentResolver().notifyChange(uri, null);
     }
     return results;
   } finally {
     db.endTransaction();
   }
 }
 /** Transactional implementation of applyBatch. */
 @Override
 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
     throws OperationApplicationException {
   ContentProviderResult[] result = new ContentProviderResult[operations.size()];
   // Opens the database object in "write" mode.
   SQLiteDatabase db = mOpenHelper.getWritableDatabase();
   // Begin a transaction
   db.beginTransaction();
   try {
     int i = 0;
     for (ContentProviderOperation operation : operations) {
       // Chain the result for back references
       result[i++] = operation.apply(this, result, i);
     }
     db.setTransactionSuccessful();
   } finally {
     db.endTransaction();
   }
   return result;
 }
 @Override
 public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
     throws OperationApplicationException {
   int ypCount = 0;
   int opCount = 0;
   mDb = mOpenHelper.getWritableDatabase();
   mDb.beginTransactionWithListener(this);
   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 (i > 0 && operation.isYieldAllowed()) {
         opCount = 0;
         if (mDb.yieldIfContendedSafely(SLEEP_AFTER_YIELD_DELAY)) {
           ypCount++;
         }
       }
       results[i] = operation.apply(this, results, i);
     }
     mDb.setTransactionSuccessful();
     return results;
   } finally {
     mApplyingBatch.set(false);
     mDb.endTransaction();
     onEndTransaction();
   }
 }