mml*_*loo 4 android android-contentresolver android-contentprovider android-loadermanager android-loader
代码:
首先是我的Uris
public static final String PACKAGE = "my.url.contentprovider";
public static final String TABLE_NAME = "NetworkTransaction";
public static final String AUTHORITY = PACKAGE + ".NetTransContentProvider";
public static final Uri BASE_URI = Uri.parse("content://"+AUTHORITY);
public static final Uri CONTENT_URI_ANY_OBSERVER = Uri.withAppendedPath(BASE_URI,TABLE_NAME+"/*");
public static final Uri CONTENT_URI_FIND_BY_ID = Uri.withAppendedPath(BASE_URI,TABLE_NAME+"/FIND/ID");
public static final Uri CONTENT_URI_INSERT_OR_REPLACE_BY_ID = Uri.withAppendedPath(BASE_URI,TABLE_NAME+"/INSERT/REPLACE/ID");
public static final Uri CONTENT_URI_INSERT_BY_ID = Uri.withAppendedPath(BASE_URI,TABLE_NAME+"/INSERT/ID");
Run Code Online (Sandbox Code Playgroud)
和我的活动代码加载器:
@Override
protected void onResume() {
super.onResume();
getSupportLoaderManager().restartLoader(NET_TRANS_LOADER_ID,mBundle,this).forceLoad();
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {
Uri uri = null;
CursorLoader cl=null;
switch (id) {
case NET_TRANS_LOADER_ID:
uri = NetTransContentProvider.CONTENT_URI_FIND_BY_ID;
cl = new CursorLoader(ChoosingUserNameActivity.this, uri, NetTransDbUtils.allColumns,
NetTransDbUtils.COLUMN_ID + " = ? ",
new String[]{String.valueOf(bundle.getLong(EXTRA_TRANSACTION_ID,-1))}, null);
break;
default:
break;
}
return cl;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
final int loaderId = loader.getId();
switch (loaderId) {
case NET_TRANS_LOADER_ID:
if(mTransactionId != null){
NetTrans netTrans = NetTransDbUtils.cursorToNetTrans(cursor);
if(netTrans != null && netTrans.getStatus() != null
&& !netTrans.getStatus().equals(NetTrans.STATUS_PENDING)){
EventBus.getDefault().post(new NetTransMsg(true, mTransactionId, netTrans.getMessage()));
}
}
break;
default:
break;
}
}
Run Code Online (Sandbox Code Playgroud)
在ExecutorService我打电话的已启动服务中运行的runnable上
mContext.getContentResolver().insert(NetTransContentProvider.CONTENT_URI_INSERT_OR_REPLACE_BY_ID, cv );
插入的值但加载器不会调用:
@Override
public Uri insert(Uri uri, ContentValues values) {
int uriType = sUriMatcher.match(uri);
switch (uriType) {
case INSERT_OR_REPLACE_BY_ID:
mDatabase.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
break;
case INSERT_BY_ID:
mDatabase.insert(TABLE_NAME, null, values);
break;
default:
break;
}
getContext().getContentResolver().notifyChange(CONTENT_URI_ANY_OBSERVER, null);
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,String sortOrder) {
Cursor cursor = mDatabase.query(TABLE_NAME,projection,selection, selectionArgs, null, null, null);
cursor.setNotificationUri(getContext().getContentResolver(), CONTENT_URI_ANY_OBSERVER);
return cursor;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是getContext().getContentResolver().notifyChange(CONTENT_URI_ANY_OBSERVER, null);在insert方法中无法使我的loader重启.
UPDATE
我创建了一个示例项目,按下按钮,NetTrans创建一个新对象并写入数据库,然后线程休眠5000毫秒并覆盖该值(以模拟网络操作).但是那个装载机没有重新启动之后.我的虫子在哪里?
如果您希望在CONTENT_URI_ANY_OBSERVER发生更改时通知已注册的观察者CONTENT_URI_FIND_BY_ID,则需要确保两件事.
首先,CONTENT_URI_ANY_OBSERVER需要成为父母CONTENT_URI_FIND_BY_ID.如果您将其视为文件系统上的文件夹,则"CONTENT_URI_ANY_OBSERVER"应在其子文件夹中包含"CONTENT_URI_FIND_BY_ID".
其次,notifyDescendants在注册内容观察者时,必须为参数传递true .
Android尝试查找匹配的内容观察者时没有外卡考虑因素(注释中提供的链接仅适用于UriMatcher).因此,要解决您的问题,您应该从您的/中删除CONTENT_URI_ANY_OBSERVER它,它应该开始匹配.你可以看到my.url.contentprovider/NetworkTransaction 现在的父母"文件夹" my.url.contentprovider/NetworkTransaction/INSERT/REPLACE/ID作为之前你有哪里my.url.contentprovider/NetworkTransaction/*.
在审核了您的示例项目后,我找到了您的问题所在的另一个区域.使用游标加载器时,游标由加载器拥有.这意味着除了迭代数据之外,你不应该改变它.在您的NetTransDbUtils.cursorToNetTrans(cursor)方法中,您将关闭光标,这将阻止CursorLoader有效监视光标数据的更改.
答案很简单:不要调用Close cursor.close()在NetTransDbUtils.cursorToNetTrans(cursor);这个用例.