0

Currently i am only retrieving max size of items -1 from the Cloud Firestore meaning if there's 3 items it will only retrieve 2. May i know what i am doing wrong for retrieving the items from collection().document().collection()?

This is my Chapter Fragment which holds the RecyclerView in xml

public class ChapterFragment extends Fragment {
    FirebaseFirestore db;
    private RecyclerView recyclerView;
    private View view;
    private Query mQuery;
    private FirestoreRecyclerAdapter adapter;
    private String documentId;

    public ChapterFragment() {

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        db = FirebaseFirestore.getInstance();
        view = inflater.inflate(R.layout.fragment_chapter, container, false);
        recyclerView = view.findViewById(R.id.chapter);
        //Query
            mQuery = db.collection("Book").document("1").collection("1").orderBy("Volume", Query.Direction.DESCENDING);
        //Options
            FirestoreRecyclerOptions<ChapterDetails> options = new FirestoreRecyclerOptions.Builder<ChapterDetails>()
                    .setQuery(mQuery,ChapterDetails.class)
                    .setLifecycleOwner(this)
                    .build();

        //ViewHolder
        adapter = new FirestoreRecyclerAdapter<ChapterDetails, ChapterDetailsViewHolder>(options) {
            @NonNull
            @Override
            public ChapterDetailsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.chapter_list, parent , false);
                return new ChapterDetailsViewHolder(view2);
            }

            @Override
            protected void onBindViewHolder(@NonNull ChapterDetailsViewHolder holder, int position, @NonNull ChapterDetails model) {
                holder.volume.setText("volume " + model.getVolume());
                holder.PDF.setText(model.getPDF());
            }
        };

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);
        return view;
    }

    private class ChapterDetailsViewHolder extends RecyclerView.ViewHolder{

        private TextView volume;
        private TextView PDF;

        public ChapterDetailsViewHolder(@NonNull View itemView) {
            super(itemView);

            volume = itemView.findViewById(R.id.volume_number);
            PDF = itemView.findViewById(R.id.pdf_text);
        }
    }
}

This is the picture of my firestore:

This is the picture of my firestore

This is my ChapterDetails

public class ChapterDetails {
    private String Volume,PDF;
    public ChapterDetails(){
        //empty
    }
    public ChapterDetails(String volume, String pdf) {
        Volume = volume;
        PDF = pdf;
    }

    public String getVolume() {
        return Volume;
    }

    public void setVolume(String volume) {
        Volume = volume;
    }

    public String getPDF() {
        return PDF;
    }

    public void setPDF(String PDF) {
        this.PDF = PDF;
    }


}

This is how it looks in the android emulator

EDIT 28/5/2020:

D/error: get failed with 
    com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document from cache. (However, this document may exist on the server. Run again without setting source to CACHE to attempt to retrieve the document from the server.)
        at com.google.firebase.firestore.core.FirestoreClient.lambda$getDocumentFromLocalCache$9(com.google.firebase:firebase-firestore@@21.4.3:197)
        at com.google.firebase.firestore.core.FirestoreClient$$Lambda$9.then(Unknown Source:0)
        at com.google.android.gms.tasks.zzd.run(Unknown Source:5)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I/TAG: Current value of i = 1
I/TAG: Current value of i = 2
I/TAG: Current value of i = 3
I/TAG: Current value of i = 4
I/TAG: Current value of i = 5
I/TAG: Current value of i = 6

apparently firestore is saying i created source CACHE? which i didn't

  • hi @Alex Mamo may i know how that answers my question? – Pandorie cho May 26 '20 at 15:30
  • In a way in which you should change the name of your fields or use an annotation. please check the duplicate. In your class you should have fields named lower case, as well in the database. – Alex Mamo May 26 '20 at 15:33
  • @AlexMamo i did as what the post said and changed them to small letter however now i can't even see a single item in recyclerview – Pandorie cho May 26 '20 at 15:47
  • @AlexMamo and previously i was able to see n-1 number of documents before hand so how was this similiar? I'm relatively new to firestore – Pandorie cho May 26 '20 at 15:48
  • @AlexMamo While the question you linked may be related, it is on its own clearly not enough to help OP. Please either also leave a comment showing how to apply the information from the link to this use-case, or provide an answer showing that in a code sample and linking to your previous answer. – Frank van Puffelen May 26 '20 at 15:58
  • @FrankvanPuffelen I see, I should have been posted as an answer. Thank. – Alex Mamo May 26 '20 at 16:10
  • 1
    @Alex: I did :) (I lost your link, but feel free to add it to the "see also" list at the end there). That said, apparently OPs problems was not about property names (no idea how that would've worked with the database structure and code) but about the number of items. But I also don't see where it would skip one of more documents. – Frank van Puffelen May 26 '20 at 16:14
  • @FrankvanPuffelen Thanks puf, just updated the "see also" list with the corresponding link. Just a guess, one item might be skipped because "Volume 1" and "Volume 2" documents share the exact same field names, while "Volume 3" it's possible to have different names. Possible something like (name and pdf). – Alex Mamo May 26 '20 at 16:41
  • 1
    Interesting hypothesis Alex. – Frank van Puffelen May 26 '20 at 16:46

1 Answers1

1

Firebase uses Java Bean style naming conventions when mapping the properties from your Java class to the fields in the Firestore database.

In this convention, your getVolume and setVolume methods map to a property/field called volume and getPDF and setPDF map to a property called pDF. You'll note that both names start with a lowercase letter, while in your document they start with an uppercase letter. And since those are different names, your class will never be able to read from this document.

You can control the mapping of properties to fields by using a `` annotation on the getters and setters. Something like this:

public class ChapterDetails {
    private String Volume,PDF;
    public ChapterDetails(){
        //empty
    }
    public ChapterDetails(String volume, String pdf) {
        Volume = volume;
        PDF = pdf;
    }

    @PropertyName("Volume")
    public String getVolume() {
        return Volume;
    }

    @PropertyName("Volume")
    public void setVolume(String volume) {
        Volume = volume;
    }

    @PropertyName("PDF")
    public String getPDF() {
        return PDF;
    }

    @PropertyName("PDF")
    public void setPDF(String PDF) {
        this.PDF = PDF;
    }
}

Also see:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • hi sorry to say this but i just tried this and it returned back the same results maxsize-1 – Pandorie cho May 26 '20 at 16:09
  • Did you see Alex' comment on why that *could* be? https://stackoverflow.com/questions/62025436/firestore-query-not-retrieving-all-items-into-firestore-recyclerview?noredirect=1#comment109704612_62025436 – Frank van Puffelen May 26 '20 at 16:47
  • hi are you talking about the volume may have different names? if so i have checked it and it all has Volume as its field – Pandorie cho May 26 '20 at 16:49