android - Android Studio Firebase 如何在 Firebase 实时数据库中插入长列表?
问题描述
我正在尝试使用以下代码在实时数据库中插入一长串产品。该列表有大约 90 种产品,但是当我尝试添加它们时,我得到的产品更少,比如缺少 2-3 种产品。有没有办法插入所有产品?我相信我得到的产品更少,因为我使用的是时间戳。
这是我的代码
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
String[] productTitles = {length of array is 93};
String[] productQuantities = {length of array is 93};
String[] productIcons = {length of array is 93};
String[] productCategories = {length of array is 93};
HashMap<String, Object> hashMap = new HashMap();
hashMap.put("discountAvailable","false");
hashMap.put("discountNote","");
hashMap.put("discountPrice","0");
hashMap.put("isAvailable","Available");
hashMap.put("isApproved","Approved");
hashMap.put("productType","Default");
hashMap.put("originalPrice","");
hashMap.put("productDescription", "");
hashMap.put("uid",""+firebaseAuth.getUid());
String timestamp;
for (int i = 0; i < productTitles.length; i++) {
timestamp = "" + System.currentTimeMillis()+1;
hashMap.put("timestamp", "" + timestamp);
hashMap.put("productTitle", "" + productTitles[i]);
hashMap.put("productCategory", "" + productCategories[i]);
hashMap.put("productQuantity","" + productQuantities[i]);
hashMap.put("productIcon",""+productIcons[i]);
hashMap.put("productId",""+timestamp);
reference.child(firebaseAuth.getUid()).child("Products").child(timestamp).setValue(hashMap);
}
解决方案
您可以用 firebase 密钥替换时间戳。
for (int i = 0; i < productTitles.length; i++) {
timestamp = "" + System.currentTimeMillis()+1;
hashMap.put("timestamp", "" + timestamp);
hashMap.put("productTitle", "" + productTitles[i]);
hashMap.put("productCategory", "" + productCategories[i]);
hashMap.put("productQuantity","" + productQuantities[i]);
hashMap.put("productIcon",""+productIcons[i]);
hashMap.put("productId",""+timestamp);
String uniqueID = reference.push().getKey();
reference.child(firebaseAuth.getUid()).child("Products")
.child(uniqueID).setValue(hashMap);
}
我不确定这是否有效。
推荐阅读
- presto - Presto:前一个月的最后一天
- amazon-web-services - 是否可以在 AWS cloudformation 中提供非 base64 字符串作为输入
- node.js - 从 Feathers nedb 迁移到 Postgres 需要对代码进行哪些更改?
- inno-setup - 通过 Inno Setup 制作的安装程序安装多个软件副本
- elasticsearch - Grafana 导数未显示正确的输出值
- sql - SQL 日期转换 HHMMSS.CCCNNNNNN 到 yyyy-mm-dd hh:mi:ss.mmm
- azure - 如何翻译应用程序授权窗口 Azure AD
- javascript - NodeJS 返回 SQL 查询以传递给另一个文件
- javascript - 角度模式形式的数组长度
- php - 尝试在自定义论坛上发布新主题时出现空白页面