2

我将尽量简化这种情况。

观察以下代码:

//... 
ArrayList<ContentProviderOperation> batch = Lists.newArrayList();

while(recordsToProcess){
    //... Grabbing data
    //... built a ContentProviderOperation variable named operation

    batch.add(operation);

    //... setting recordsToProcess to true or false
}

try {
    mContext.getContentResolver().applyBatch(Contract.CONTENT_AUTHORITY, batch);

    } catch (RemoteException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    } catch (OperationApplicationException e) {
        throw new RuntimeException("Problem applying batch operation", e);
    }
}

在此过程中,会抛出 OperationApplicationException,如下所示:

... Problem applying batch operation
... android.content.OperationApplicationException: insert failed
... Stack Trace ....

我了解此异常是“如果插入失败或受影响的行数与预期计数不匹配时抛出”和/或“当 ContentProviderOperation 的应用程序由于指定的约束而失败时抛出”。

我的数据库模式几乎没有任何约束,并且操作中的数据是有效的。

有什么方法可以获取有关引发异常的原因的更多信息?什么约束失败了?ETC...?

4

1 回答 1

3

对于调试,我将从这样的事情开始

   {
   .........
        int i = 0;
        for (ContentProviderOperation op : ops) {
            applySingle(WorkoutProvider.AUTHORITY, op);
            Log.e(getTag(), Integer.toString(i));
            i++;
        }
    }


protected synchronized void applySingle(String authority, ContentProviderOperation op) {
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(op);
    try {
        App.getInstance().getContentResolver().applyBatch(authority, ops);
    } catch (RemoteException e) {
        Log.e(getTag(), "Error updating workout id to server's", e);
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        Log.e(getTag(), "Error updating workout id to server's", e);
        e.printStackTrace();
    }
}

然后我会建议在 for 循环之前进行调试并查看失败的操作。

于 2013-10-04T17:53:34.660 回答