简体   繁体   中英

OnCreateLoader not called randomly

In my IM application there is a chats window where loader.onContentChanged is called whenever message is sent or received. It works perfectly fine but sometimes randomly even after calling 'loader.onContentChanged' there are no calls to onCreateLoader and onLoadFinished. This is very random but users get a weird behavior wherein they click send and message disappears. Logs indicate that initLoader is called but logs from onCreateLoader are missing. I am using recycler view to show list of messages, but that code is not a issue.

I don't know the reason but this is how the defect got fixed:

Old code:

private void initLoader(){
    LoaderManager.LoaderCallbacks<Cursor> callbacks = this;
    LoaderManager lm = getSupportLoaderManager();
    if (mLoader == null) {
        mLoader = lm.initLoader(LOADER_ID, null, callbacks);
    } else {
        mLoader.onContentChanged();
    }
}`

New code:
`private void initLoader() {
    LoaderManager.LoaderCallbacks<Cursor> callbacks = this;
    LoaderManager lm = getSupportLoaderManager();
    if (mLoader == null) {
        mLoader = lm.initLoader(LOADER_ID, null, callbacks);
    } else {
        if(mLoader.isStarted()) {
            mLoader.onContentChanged();
        }else {
            try {
                mLoader = lm.restartLoader(LOADER_ID, null, callbacks);
            }catch( Exception e ){
                mLoader = null;
                initLoader();
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM