使用FirestorePagingAdapter

时间:2019-10-08 15:21:33

标签: android firebase memory-leaks firebaseui leakcanary

我正在使用FirestorePagingAdapter从Firebase Firestore数据库加载数据。使用leakCanary可以显示 BorrowedBooksFragment $ 1.mObservable 中的内存泄漏。 FirestorePagingAdapteronStart()处自动开始平移并在onStop()处停止监听,因此我不必担心生命周期。 enter image description here

我的代码正在关注

public class BorrowedBooksFragment extends Fragment {


private void fetchBooks()
    {
        Query bookQuery= FirebaseDatabase
                .getInstance()
                .getReference()
                .child("Books")
                .orderByChild(ClassAttribute.Book.CURRENT_OWNER_ID)
                .equalTo(user.getUid());

        FirebaseRecyclerOptions<Book> options =
                new FirebaseRecyclerOptions.Builder<Book>()
                        .setQuery(bookQuery,Book.class)
                        .setLifecycleOwner(this)
                        .build();
       adapter= new FirebaseRecyclerAdapter<Book, BookViewHolder>(options) {
            @NonNull
            @Override
            public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.book_row_item, parent, false);

                return new BookViewHolder(view);
            }
     };
    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();

    }

    @Override
    public void onStop() {
        super.onStop();
        if(adapter != null) {
            adapter.stopListening();
        }
    }
}

0 个答案:

没有答案