Android Firestore convert array of document references to List

后端 未结 1 1197
半阙折子戏
半阙折子戏 2020-11-27 23:07

In my Firestore I\'ve got a users collection, within which the documents could have a bookmarks field, which is an array of references:

1条回答
  •  难免孤独
    2020-11-27 23:58

    Yes it is. Please see the following lines of code:

    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser != null) {
        String uid = firebaseUser.getUid();
        rootRef.collection("users").document(uid).get().addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        List list = (List) document.get("bookmarks");
                        List> tasks = new ArrayList<>();
                        for (DocumentReference documentReference : list) {
                            Task documentSnapshotTask = documentReference.get();
                            tasks.add(documentSnapshotTask);
                        }
                        Tasks.whenAllSuccess(tasks).addOnSuccessListener(new OnSuccessListener>() {
                            @Override
                            public void onSuccess(List list) {
                                //Do what you need to do with your list
                                for (Object object : list) {
                                    TeacherPojo tp = ((DocumentSnapshot) object).toObject(TeacherPojo.class);
                                    Log.d("TAG", tp.getFirstName());
                                }
                            }
                        });
                    }
                }
            }
        });
    }
    
    
    

    So the List list is actually the list that contains objects of type TeacherPojo.

    0 讨论(0)
    提交回复
    热议问题