vendredi 27 février 2015

ContentResolver.insert returns null EVENTUALLY

I have an App which inserts data from activity and background servicer from multiply threads into 4-5 tables using custom ContentProvider.


this is AndroidManifest.xml (I don't declare android:read(write)Permission and for provider):



<application
...
>
<provider
android:name=".content.provider.DefaultProvider"
android:authorities="com.provider.auth"
android:exported="false"
android:grantUriPermissions="true"
android:label="Default Provider"
/>
</application>


this is insert() implementation inside DefaultProvider class:



@Override
public Uri insert(Uri uri, ContentValues values) {
UriDetail uriDetail = URI_RESOLVER.match(uri);

if (uriDetail == null || uriDetail.isSingleItem()){
throw new IllegalArgumentException("Unsupported URI: " + uri);
}

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
long id = db.insert(uriDetail.getScheme().getName(), null, values);

if (id != -1){
Uri idUri;
try {
idUri = getUriForId(id, uri);
} catch (SQLException e) {
e.printStackTrace();
throw e;
}

capacityManager.afterInsert(URI_RESOLVER, uriDetail, id);
if (idUri == null){
throw new NullPointerException(
"Insert into " + uriDetail.getScheme().getName() + " returned id = " + id+ " but no URI with ID was acquired!"+
"\nUri: "+uri+
"\nContentValues: "+values.toString()
);
}
return idUri;
}else{
throw new OperationApplicationException("Insert into "+uriDetail.getScheme().getName()+" returned id = "+id + "\n values: "+values.toString()));
}

return null;
}


UriResolver and UriDetail are helper-classes used to match URI with actual table name inside sqlite database. Also getUriForId() simply does ContentUris.withAppendedId(uri, id); and few validations. In any case - this insert() should throw Exception or return valid Uri, but not NULL. So I do regular



context.getContentResolver().insert(uri, contentValues)


receive URI and I'm happy, but rarely(!) insert() returns null without exception - it looks like DefaultProvider.insert() was not triggered at all and no record is actually inserted into db. My app works on 2.3+ devices but most of those errors I get from 4.2.2 - 4.4. Why this magic happens? Is this android issue or my mistake?


Aucun commentaire:

Enregistrer un commentaire