Realm has the ability to create asynchronous requests . To do this, instead of findAll() call findAllAsync() .
I will correct your interface, because both of these methods do not return a List , but RealmResults is an object of the Future standard:
public interface QuoteRepository { RealmResults<QuoteText> getListOfQuoteTextAsync(); }
In order to receive notification of the end of the download, you need to add a subscription to the RealmResults instance:
RealmResults<QuoteText> result = quoteDataRepository.getListOfQuoteTextAsync(); result.addChangeListener(new RealmChangeListener() { @Override public void onChange(RealmResults<User> results) { // метод будет вызван, когда запрос будет выполнен или при обновлении данных } });
In addition, you can make sure that the download is complete by calling the isLoaded() method:
if (result.isLoaded()) { // данные загружены }
Getting the result
To work with query results (including asynchronous), Realm provides specialized adapters that need to be added to dependencies in build.gradle :
dependencies { compile 'io.realm:android-adapters:1.4.0' }
After that you need to create a successor from RealmRecyclerViewAdapter , which will work with your ViewHolder . I will demonstrate the use of the example from the documentation.
public class MyFragment extends Fragment { private Realm realm; private RecyclerView recyclerView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { realm = Realm.getDefaultInstance(); View root = inflater.inflate(R.layout.fragment_view, container, false); recyclerView = (RecyclerView) root.findViewById(R.id.recycler_view); // установка Вашего адаптера для RecyclerView recyclerView.setAdapter(new MyRecyclerViewAdapter(getActivity(), // установка результата асинхронного запроса quoteDataRepository.getListOfQuoteTextAsync())); // ... return root; } @Override public void onDestroyView() { super.onDestroyView(); realm.close(); } }
In general, to get (and display) the result inside Fragment / Activity you need to subscribe to RealmResults . This should be done in the calling code in order to be able to unsubscribe from notifications (since no one except the calling code knows when the request for it is already irrelevant). This option will look like this:
public class MyFragment extends Fragment { private Realm realm; private RealmResults results; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { realm = Realm.getDefaultInstance(); View root = inflater.inflate(R.layout.fragment_view, container, false); results = quoteDataRepository.getListOfQuoteTextAsync(); // добавление подписки на получение результата results.addChangeListener(new RealmChangeListener() { @Override public void onChange(RealmResults<User> results) { // метод будет вызван, когда запрос будет выполнен или при обновлении данных // здесь можно обновлять экран или делать другой полезный код } }); return root; } @Override public void onDestroyView() { super.onDestroyView(); // при уничтожении фрагмента нужно отписаться от уведомлений results.removeChangeListeners(); realm.close(); } }
RealmResults- you will work with it. Transforming it into aListmeaningless and merciless - the very essence of working with the database is lost, especially considering that in Realm the selection ofRealmResultsis related to the data in the database and changes in the data in the sample change the data in the database itself. - pavlofff