クラウドFirestoreから複雑なドキュメントを取得します

Charithaデ・シルバ:

私はクラウドFirestoreから複雑なオブジェクトのセットを取得しようとしています。

次のようにデータが格納されます。

データベース構造

本文書は、著者および翻訳者のサブコレクションを持っています。(私はBookオブジェクトの他のすべての属性を省略して、問題を理解することが必要であるものを保持している)、次のように本の完全なリストを取得するために、データベースへの呼び出しがあります:

public void getBooks(final BookRetrievable bookRetrievable) {
    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    ArrayList<Book> books = new ArrayList<>();

    firebaseFirestore.collection("books")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (DocumentSnapshot documentSnapshot : task.getResult()) {
                            Book book = new Book();
                            book.setId(documentSnapshot.getString("id"));
                            book.setName(documentSnapshot.getString("name"));
                            book.setStatus(Status.valueOf(documentSnapshot.getString("status")));
                            documentSnapshot.getReference().collection("authors")
                                    .get()
                                    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                            if (task.isSuccessful()) {
                                                ArrayList<Author> authors = new ArrayList<>();
                                                for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                                    Author author = new Author();
                                                    author.setId(documentSnapshot.getString("id"));
                                                    author.setNameInEnglish(documentSnapshot.getString("nameInEnglish"));
                                                    author.setNameInSinhalese(documentSnapshot.getString("nameInSinhalese"));
                                                    author.setWebsite(documentSnapshot.getString("website"));
                                                    authors.add(author);
                                                }

                                                book.setAuthors(authors);

                                                documentSnapshot.getReference().collection("translators")
                                                        .get()
                                                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                                            @Override
                                                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                                                if (task.isSuccessful()) {
                                                                    ArrayList<Translator> translators = new ArrayList<>();
                                                                    for (DocumentSnapshot documentSnapshot : task.getResult()) {
                                                                        Translator translator = new Translator();
                                                                        translator.setId(documentSnapshot.getString("id"));
                                                                        translator.setNameInEnglish(documentSnapshot.getString("nameInEnglish"));
                                                                        translator.setNameInSinhalese(documentSnapshot.getString("nameInSinhalese"));
                                                                        translator.setWebsite(documentSnapshot.getString("website"));
                                                                        translators.add(translator);
                                                                    }

                                                                    book.setTranslators(translators);

                                                                    books.add(book);

                                                                    books.sort((Book b1, Book b2) -> b1.getSeriesNo().compareTo(b2.getSeriesNo()));
                                                                    bookRetrievable.onCallback(books);
                                                                } else {
                                                                    bookRetrievable.onCallback(null);
                                                                }
                                                            }
                                                        });
                                            } else {
                                                bookRetrievable.onCallback(null);
                                            }
                                        }
                                    });
                        }
                    } else {
                        bookRetrievable.onCallback(null);
                    }
                }
            });
}

私はログを置く場合は、検索された本の数限りコールバックがあるように思えます。したがって、コールは1つのだけの本あたりのバックとあらゆる著者と訳者文書ごとに存在するように、このコードを最適化する方法はありますか?

前もって感謝します!

アレックス・マモ:

はいあります。すべてのブックオブジェクトを取得するためには、オブジェクトの作成する必要はありませんBookクラスは、あなたからそれを得ることができDocumentSnapshot、このようなオブジェクト:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference booksRef = rootRef.collection("books");
booksRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                Book book = document.toObject(Book.class);
                //Do whay you need to do with your Book object
            }
        }
    }
});

あなたが特定の書籍の著者や翻訳を取得する必要がある場合は、あなたの中に追加しBookたクラス2つの以上fileds、authorおよびtranslator対応する公開ゲッターを。取得するには、のは、ブックオブジェクトからの翻訳者は、あなたが次のコード行を使用することができましょう。

Translator translator = book.getTranslator();

同様に、あなたは著者で行うことができます。

Author autothor = book.getAuthor();

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=218843&siteId=1