gpt4 book ai didi

java - 将数组读/写到 firebase 的最佳/更好方法

转载 作者:行者123 更新时间:2023-12-02 08:58:09 26 4
gpt4 key购买 nike

我试图找到如何从 Firebase 中写入/读取数组,最终得到了这个结果。当我写作时,我会:

String RestaurantIDs = "";
for(String RestaurantID : category.getRestaurantIDs()) { RestaurantIDs += RestaurantID + ","; }
DataToSave.put(CategoryFireBase.RestaurantIDs, "[" + RestaurantIDs + "]");

当我阅读时,我会:

ArrayList<String> restaurantIDs = new ArrayList<>();
String[] Temp_restaurantIDs = document.get(CategoryFireBase.RestaurantIDs).toString().replace("[","").replace("]","").split(",");
for (String value : Temp_restaurantIDs){restaurantIDs.add(value);}

我有两个问题。

  1. 我可以将其保存为长字符串而不是数组吗?
  2. 如果有一种方法可以像实际数组一样读写数组,是不是比这个更好?

最佳答案

  1. Is it ok that I'm saving it as long string instead of arrays?

这没有什么问题,但是使用数组更方便。例如,如果数据库中有一个餐厅 id 数组,当您进行 get() 调用时,您可以直接以列表形式获取该数组,而无需拆分那个长字符串。

  1. If there is a way to read and write the array as an actual array, is it better then this?

这是不是更好,因为您可以使用专门的方法来更新或删除数组中的元素,如官方文档中所述:

关于java - 将数组读/写到 firebase 的最佳/更好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60365575/

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