0

Here is what is stored in Firebase:

  "73668" : {
    "id" : "73668",
    "price" : "Price",
    "product1" : "1. Product 1",
    "product2" : "2. Product 2",
    "product3" : "3. Product 3",
    "status" : "not ready"
  },
  "a2521" : {
    "id" : "a2521",
    "price" : "Price",
    "product1" : "1. Product 1",
    "product2" : "2. Product 2",
    "product3" : "3. Product 3",
    "status" : "not ready"
  },
  "a719b" : {
    "id" : "a719b",
    "price" : "Price",
    "product1" : "1. Product 4",
    "product2" : "2. Product 5",
    "product3" : "3. Product 6",
    "status" : "not ready"
  }
}

Here is how it was fetched in the application: enter image description here

The code I am using:

public ArrayList<Order> getOrders() {

  final Firebase myFirebaseRef = new Firebase("https://torrid-heat-2650.firebaseio.com/orders");
  myFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
    public void onDataChange(DataSnapshot snapshot) {
      for (DataSnapshot child : snapshot.getChildren()) {
        Order order = new Order();
        order.setProduct1(child.child("product1").getValue().toString());
        order.setProduct2(child.child("product2").getValue().toString());
        order.setProduct3(child.child("product3").getValue().toString());
        order.setId(child.child("id").getValue().toString());
        order.setPrice(child.child("price").getValue().toString());

        itemsList.add(order);
      }
    }

    public void onCancelled(FirebaseError firebaseError) {
    }

  });
  System.out.println(itemsList.size());
  return itemsList;
}

The same operation returns empty arraylist on the Android device. The interesting thing is that it outputs 0 in both cases, however returns non-null arraylist. What could result 2 different outputs in the same code? Thank you.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    You've included a picture of the JSON tree in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export button in your Firebase database. Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Mar 28 '16 at 14:45
  • 1
    Yes you need to edit your question with the JSON tree which makes it more solvable in case of solving firebase problems. But, anyway, I would like to suggest something more. Please have a look at this answer which is the neat and clean way to retrieve the data you've got there. http://stackoverflow.com/a/36264522/3145960. And yes, this is very unusual that your code is working in emulator and not working in real device. Can you please have a close look in your logcat and check if its getting an exception for any reason? It would be better if you could provide any repeated logs from your logcat – Reaz Murshed Mar 28 '16 at 18:32
  • @FrankvanPuffelen thank you for your suggestion, I will follow it in the future questions. – Бейбут Тукибаев Mar 29 '16 at 03:46
  • The fact that `System.out.println(itemsList.size());` prints `0` is expected, since the data hasn't loaded yet. See http://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener – Frank van Puffelen Mar 29 '16 at 03:55
  • @FrankvanPuffelen this fact makes me think that the reason the emulator loads data is that it is much slower than android device. Am I correct? – Бейбут Тукибаев Mar 29 '16 at 06:17
  • Nope, that could only have explained if one prints 0 and the other not. It shouldn't make any difference in *whether* a device gets the data. I'm lost on that too. – Frank van Puffelen Mar 29 '16 at 13:06

0 Answers0