I want to load some data from local database, I am using AsynctaskLoader for that purpose. But while data is loading I cant interact with the screen. Whole UI of freezes. I did some research and found that AsyncTaskLoader should be initialized on the worker thread to move the whole process on the worker thread, I tried working that as well but again im getting is freeze in UI.
Here is my AsyncTaskLoader class
public class ArticleDatabaseLoader extends AsyncTaskLoader<List<Article>> {
private int code = 0;
public ArticleDatabaseLoader(Context context, int code) {
super(context);
this.code = code;
}
@Override
public List<Article> loadInBackground() {
List<Article> articleList = new ArrayList<>();
ArticleTable articleTable = new ArticleTable(getContext());
ArticleCategory categoryTable = new ArticleCategory(getContext());
switch (code) {
case ArticleList:
articleTable.open();
categoryTable.open();
List<String> selected_category = new ArrayList<>();
for (int i = 0; i < 5; i++) { /* saving all the selected categories */
if (getCategoryStatus(getContext(), categories[i]) == 1) {
selected_category.add(categories[i]);
}
}
StringBuilder where_query = new StringBuilder();
if (selected_category.size() > 0){ /* building the where query for sql */
for (int i = 0; i < selected_category.size(); i++) {
if (i == selected_category.size() - 1) {
where_query.append(String.format("%s = '%s'",
DBColumn.ArticleCategoryColumn.category_id,
selected_category.get(i)));
} else {
where_query.append(String.format("%s = '%s' OR ",
DBColumn.ArticleCategoryColumn.category_id,
selected_category.get(i)));
}
}
Cursor selectedCursor = categoryTable.getArticles(where_query.toString());
if (selectedCursor != null && selectedCursor.moveToFirst() && selectedCursor.getCount() > 0) {
do {
String articleId = selectedCursor.getString(selectedCursor.getColumnIndex(DBColumn.ArticleCategoryColumn.article_id));
Cursor articleCursor = articleTable.getArticleById(articleId);
if (articleCursor != null && articleCursor.moveToFirst()) {
articleList.add(articleTable.articleObject(getContext(),articleCursor));
}
articleCursor.close();
} while (selectedCursor.moveToNext());
}
if (selectedCursor != null)
selectedCursor.close();
} else {
Cursor articleListCursor = articleTable.getAllArticles();
if (articleListCursor != null && articleListCursor.moveToFirst() && articleListCursor.getCount() > 0) {
do {
articleList.add(articleTable.articleObject(getContext(),articleListCursor));
} while (articleListCursor.moveToNext());
}
if (articleListCursor != null)
articleListCursor.close();
}
articleTable.close();
categoryTable.close();
return articleList;
}
return null;
}
}
and here is my onCreateMethod
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArticleTable articleTable = new ArticleTable(this);
articleTable.open();
if (articleTable.getAllArticles() != null) {
/* load the data from local database */
getSupportLoaderManager().initLoader(Loader_ID, null, ArticleListActivity.this).forceLoad();
} else {
ApiManager.getArticles(this, this);
}
articleTable.close();
}