0

Okay, so I have an object with a list of Firebase users within it which i've been able to create the object, the problem I'm having is the nested object need information from elsewhere in the database. how would i go about retrieving data for each child node and return one object with the nested object? below is the database structure with a better explanation and some code

https://i.stack.imgur.com/OPgAH.png

so, in -(Collabs) > (User_uid) each push id is an Object and for each 'User' in (Artists) I need to retrieve an image from -(Users) > (User_uid) > (image) and return one object. Apologies in advanced if this seems a bit ignorant, as I'm still a little new to this. thank you!

public void getAllCollabs() {
    FirebaseUser current_user = auth.getCurrentUser();
    String current_uid = current_user.getUid();
    DatabaseReference db_collabs = db_root.child("Collabs").child(current_uid);
    final ArrayList<Collab> collabs = new ArrayList<>();
    Query collab_query = db_collabs.orderByKey();
    collab_query.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            ArrayList<String> artists = new ArrayList<>();
            String title = dataSnapshot.child("mTitle").getValue().toString();
            String content = dataSnapshot.child("mContent").getValue().toString();
            Long date_time = dataSnapshot.child("mDateTime").getValue(Long.class);
            String push_id = dataSnapshot.child("push_id").getValue().toString();
            String creator = dataSnapshot.child("creator").getValue().toString();
            Iterable<DataSnapshot> collab_artists = dataSnapshot.child("Artists").getChildren();
            for (DataSnapshot user : collab_artists) {
                artists.add(user.getKey())
            }
            Collab collab = new Collab(date_time, title, content, creator, push_id, null);
            collabs.add(collab);
            loadCollabResults.collabsLoaded(collabs, true);
        }
  • Check **[this](https://stackoverflow.com/questions/47847694/how-to-return-datasnapshot-value-as-a-result-of-a-method/47853774)** out. – Alex Mamo Jan 25 '19 at 08:51

1 Answers1

0

Since you grab the artist ID's and store them in the artists array, you can grab the user id and pull the image URL using that.

for(String user : artists){
    db_root.child("Users").child(user).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String image = dataSnapshot.child('image').getValue().toString();
            db_root.child("Collabs").child(collabID).child(-Where the image is going-).setValue(image);
        }
    }
}

Hope this is what your looking for!

Logan Rodie
  • 673
  • 4
  • 12
  • This was my approach, the problem I'm running into is the object would be sent to the activity before the onDataChange call is finished, leaving the Objects artists array empty. I'm not trying to re store the image to the database, I'm trying to grab the images when creating the object, thank you for taking your time to help btw :) – ShadowSuave Jan 25 '19 at 00:32