如何查看UI里面的数据是否存在于table里面?通过使用 Loader 还是仅使用 query()?

How to check inside UI whether data exist in a table? By using Loader or just query()?

如何查看UI里面的数据是否存在于table中? 通过使用 Loader 或仅使用 query()?

哪种方式更好?

1) 使用加载程序。

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        getActivity().getSupportLoaderManager().restartLoader(POPULATE_CATEGORY_LIST_VIEW_LOADER, null, this);

    } 


        @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

        switch (loader.getId()) {
            case POPULATE_CATEGORY_LIST_VIEW_LOADER:

                if (!data.moveToFirst()) {

                    // load data from server and store data to db

                }

                break;
            default:
                break;
        }
    }

2) 使用来自 UI

的 getContentResolver().query()
        Cursor locationCursor = getContext().getContentResolver().query(
                MenuContract.CategoryEntry.CONTENT_URI,
                new String[]{MenuContract.CategoryEntry._ID},
                null,
                null,
                null);

        if(!locationCursor.moveToFirst()) {
            // load data from server and store data to db
        }

我只需要检查数据是否存在id db。然后,如果table中没有数据,则从服务器加载数据并放入适配器,否则只将数据放入适配器。 如何正确使用加载程序?

我了解到这两种方式都需要等到数据库给出答案。因此,也许仅查询 Cursor 就足以完成此任务,或者直接从 UI 线程查询数据库是不好的做法?

强烈建议在单独的线程中执行数据库查询,但如果您查询的数据非常小,那么您可以在 UI 线程中执行而不会导致延迟。