gpt4 book ai didi

java - 从 Cloud Firestore 检索复杂文档

转载 作者:太空狗 更新时间:2023-10-29 13:47:33 24 4
gpt4 key购买 nike

我正在尝试从 Cloud Firestore 中检索一组复杂的对象。

数据存储如下:

database structure

书籍文档有作者和译者子集合。为了检索完整的图书列表,按如下方式调用数据库(我省略了 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);
}
}
});
}

但是,如果我放置日志,似乎有与检索到的书籍数量一样多的回调。因此,有没有一种方法可以优化此代码,使每本书和每个作者和译者文档只有一个回调?

提前致谢!

最佳答案

是的,有。为了得到所有的书对象,不需要创建一个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 类中添加另外两个字段,authortranslator 和相应的公共(public) setter/getter 。要从图书对象中获取翻译器,您可以使用以下代码行:

Translator translator = book.getTranslator();

以与作者相同的方式。

Author autothor = book.getAuthor();

关于java - 从 Cloud Firestore 检索复杂文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com