好吧,我有一个扩展AsyncQueryHandler的Repository类,我通过它完成所有的CRUD操作,但偶尔也不会执行startQuery方法。
以下是我首先启动应用程序并开始使用非零令牌查询数据库中的所有文章。
但是,如果我试图用每个查询实例化新的匿名AsyncQueryHandler,这个问题不会发生,但我需要知道它为什么会发生?
将AsyncQueryHandler用于多个查询是否正常,或者它应该像AsyncTask一样只使用一次?
public class ArticlesRepository extends AsyncQueryHandler implements ArticleRepositoryActions {
private static Context mContext;
private int queryToken=1;
private final int SEARCH_TOKEN=202;
private final int CRUD_TOKEN=1001;
private Site[]mSites=null;
private SparseArray<Pair<`ArticlesRepositoryObserver`,Site[]>> mListeners=new SparseArray<>();
private SparseArray<ArticlesRepositoryObserver> queryListeners=new SparseArray<>();
private SparseArray<FeedMeArticle> allArticles;
private final String SORT_CRITERIA=Contracts.ArticleEntry.COLUMN_DATE+" DESC";
private final int INITIALIZE_TOKEN =101;
private static ArticlesRepository repoInstance;
private static ArticlesObserver observer;
private boolean freshData;
private ArticlesRepository(ContentResolver cr) {
super(cr);
if(PrefUtils.isInitialized(mContext))
queryArticles(INITIALIZE_TOKEN);
refreshData();
observer=new ArticlesObserver(new android.os.Handler(Looper.getMainLooper()),this);
}
public static void destroyInstance(Context context) {
if(context.equals(mContext)){
mContext=null;
context.getContentResolver()
.unregisterContentObserver(observer);
Log.d("FFFF Observer Not Exits",context.toString()+"");
}
// repoInstance = null;
}
public static ArticlesRepository getInstance(Context context){
mContext=context;
if(repoInstance==null)
repoInstance=new ArticlesRepository(context.getContentResolver());
Log.d("FFFF Observer Exist",context.toString()+"");
context.getContentResolver()
.registerContentObserver(Contracts.ArticleEntry.CONTENT_URI,false,observer);
return repoInstance;
}
public void setListener(ArticlesRepositoryObserver mListener,Site[]sites) {
mListeners.put(mListener.hashCode(), new Pair<>(mListener, sites));
}
public void unsetListener(ArticlesRepositoryObserver mListener) {
mListeners.remove(mListener.hashCode());
}
@Override
public void onLocalDataChanged() {
Log.d("onLocalDataChanged","FFFF DataChanged");
if(freshData){
Log.d("onLocalDataChanged","FFFF FreshData");
Log.d("onLocalDataChanged","FFFF listeners "+mListeners.size());
queryArticles(INITIALIZE_TOKEN);
freshData=false;
}
}
private int insertValues(Uri uri, ContentValues[]cvs){
for (ContentValues cv:cvs) {
super.startInsert(CRUD_TOKEN,null,uri,cv);
}
return FeedMeApp.getContext().getContentResolver().bulkInsert(uri,cvs);
}
private void queryArticles(int token){
if(mSites==null){
startQuery(token,null, Contracts.ArticleEntry.CONTENT_URI,null,null,null,SORT_CRITERIA);
}
else {
String []selections=new String[mSites.length];
for(int i=0;i<selections.length;i++){
selections[i]=mSites[i].getID()+"";
}
startQuery(token,null,Contracts.ArticleEntry.CONTENT_URI,null,
Contracts.ArticleEntry._ID+"=?",selections,SORT_CRITERIA);
}
}