I've made a List of POJO Objects that get values from my Cloud Firestore database. This List is itself a Class having in mind to retrieve attributes everytime I create a new List. The thing is that the List is not filled in the on create activity but time after. I've tried adding loops, tasks, waits, but not getting the result desired. Here is how I retrieve my data satisfactorally:
private void retrieveData(){
reference = firestore.collection("attractions");
reference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {
attractionList.clear();
for (DocumentSnapshot snapshot : queryDocumentSnapshots) {
Attraction attraction = snapshot.toObject(Attraction.class);
addAttractions(attraction);
}
}
});
}
I retrieve the data everytime I create a new Object:
public AttractionList() {
firestore.setFirestoreSettings(settings);
reference = firestore.collection("attractions");
attractionList = new ArrayList<>();
retrieveData();
}
Then, if I check the List size on my Activity on create:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attraction);
Toolbar toolbar = findViewById(R.id.tb_attraction);
setSupportActionBar(toolbar);
//Creating POJO List and retrieving data
attractionList = new AttractionList();
Log.e("List Size: ", ""+attractionList.getAttractionList().size());
//List Size: 0
}
But if I use the same Log time after, in any button:
@Override
public void onBackPressed(){
Log.e("List Size: ", ""+attractionList.getAttractionList().size());
//List Size: 16
}
I don't really want to retrieve data in every single Activity or creating intents to bring the Object between Activities. There is any way I could fix this?
PS: This is my first post. Sorry If I've haven't done it on the right way. Thank's a lot.
You cannot use something now that hasn't been loaded yet. Firestore loads data
asynchronously
, since it may take some time for this. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before that data is available. If you want to pass a list of objects of typeAttraction
to another method, just call that method insideonSuccess()
method and pass that list as an argument. So a quick fix would be this:So remember,
onSuccess()
method has an asynchronous behaviour, which means that is called even before you are getting the data from your database. If you want to use theattractionsList
outside that method, you need to create your owncallback
. To achieve this, first you need to create an interface like this:Then you need to create a method that is actually getting the data from the database. This method should look like this:
In the end just simply call
readData()
method and pass an instance of theMyCallback
interface as an argument wherever you need it like this:This is the only way in which you can use that
attractionsList
object outsideonSuccess()
method. For more informations, you can take also a look at this video.