推送到 Firebase 实时数据库中的数据列表的最大往返次数?
Maximum number of roundtrips for pushing to a list of data in Firebase Real Time Database?
在我的 angular 4 应用程序中,我使用 push()
方法将数据附加到 Firebase 实时数据库中的数据列表。请参阅 Firebase 文档:https://firebase.google.com/docs/database/web/lists-of-data
每次将新子添加到指定的 Firebase 引用时,push()
方法都会生成一个唯一键。这是一个数组 'market',由 250 个对象组成,键为 symbol, price, balance:
market = [
{ symbol: '1ST', price_usd: 1, balance: 100},
{ symbol: '2ND', price_usd: 2, balance: 300},
{ symbol: '3RD', price_usd: 3, balance: 400}
// etc etc for a total of 250 objects
];
我在 Angular 4 组件中编写了以下代码:
import {AngularFireDatabase} from 'angularfire2/database';
import * as firebase from 'firebase';
constructor(private db: AngularFireDatabase) {}
storeTicker() {
const timestamp = firebase.database.ServerValue.TIMESTAMP;
for (let object of market) {
const path = object['symbol'];
const itemsRef = this.db.list('Tickers/' + path);
itemsRef.push({ symbol: path, price_usd: object['price_usd'], balance: object['balance'], time: timestamp});
}
}
单击按钮时执行代码,这将为每个参考生成一个时间序列。
我的 Firebase 实时数据库如下所示:
{ "Tickers" : {
"1ST" : {
"-Kx8gN5uZALd5BP2GCH6" : {
"balance" : 0,
"price_usd" : 0.25583,
"symbol" : "1ST",
"time" : 1508769890908
}},
"2ND" : {
"-Kx8Z0xCl7ONZERk1ICo" : {
"balance" : 0,
"price_usd" : 0.0253829,
"symbol" : "2ND",
"time" : 1508769890908
}},
"3RD" : {
"-Kx8gN5FEZEPsOe8S-Tw" : {
"balance" : 0,
"price_usd" : 0.767354,
"symbol" : "3RD",
"time" : 1508769890908
}},
// for a total of 250 paths
}}
以这种方式对 push()
的每次调用都会导致到 Firebase 的往返。问题是 Firebase 只对前 100 个引用执行 push()
。
我在 Firebase 文档中找不到往返次数的限制:https://firebase.google.com/docs/database/usage/limits
我在 Whosebug 上发现了一个类似的问题,但我不明白更新部分。
我的两个问题是:
Firebase 中是否有最大往返次数或是否存在其他错误?
如何通过将代码合并到多位置更新中来使我的代码正常工作?
使用不带参数的 push()
方法将在本地生成一个唯一的 ID,并且不会导致对数据库的网络操作:
If you provide a value to push()
, the value will be written to the
generated location. If you don't pass a value, nothing will be written
to the Database and the child will remain empty (but you can use the
Reference
elsewhere).
因此,您可以使用它为需要推送到数据库的每个项目获取唯一 ID,将其存储在数组中,然后使用 update()
将所有项目同时发送到数据库多位置更新,例如:
storeTicker() {
const timestamp = firebase.database.ServerValue.TIMESTAMP;
var updates = {};
for (let object of market) {
const key = this.db.list('/Tickers').push().key; // Get a new unique key (locally, no network activity)
const path = object['symbol'] + '/' + key;
updates[path] = {symbol: object['symbol'], price_usd: object['price_usd'], balance: object['balance'], time: timestamp};
}
this.db.object('/Tickers').update(updates);
}
update()
method (similar to the official Firebase JavaScript SDK update()
method) 通过将整个数组发送到指定位置来工作:
The values
argument contains multiple property-value pairs that will be written to the Database together. Each child property can either be a simple property (for example, "name") or a relative path (for example, "name/first") from the current location to the data to update.
有关这方面的文档示例,请参阅 updating data using AngularFire or update specific fields using the JavaScript SDK。
在我的 angular 4 应用程序中,我使用 push()
方法将数据附加到 Firebase 实时数据库中的数据列表。请参阅 Firebase 文档:https://firebase.google.com/docs/database/web/lists-of-data
每次将新子添加到指定的 Firebase 引用时,push()
方法都会生成一个唯一键。这是一个数组 'market',由 250 个对象组成,键为 symbol, price, balance:
market = [
{ symbol: '1ST', price_usd: 1, balance: 100},
{ symbol: '2ND', price_usd: 2, balance: 300},
{ symbol: '3RD', price_usd: 3, balance: 400}
// etc etc for a total of 250 objects
];
我在 Angular 4 组件中编写了以下代码:
import {AngularFireDatabase} from 'angularfire2/database';
import * as firebase from 'firebase';
constructor(private db: AngularFireDatabase) {}
storeTicker() {
const timestamp = firebase.database.ServerValue.TIMESTAMP;
for (let object of market) {
const path = object['symbol'];
const itemsRef = this.db.list('Tickers/' + path);
itemsRef.push({ symbol: path, price_usd: object['price_usd'], balance: object['balance'], time: timestamp});
}
}
单击按钮时执行代码,这将为每个参考生成一个时间序列。
我的 Firebase 实时数据库如下所示:
{ "Tickers" : {
"1ST" : {
"-Kx8gN5uZALd5BP2GCH6" : {
"balance" : 0,
"price_usd" : 0.25583,
"symbol" : "1ST",
"time" : 1508769890908
}},
"2ND" : {
"-Kx8Z0xCl7ONZERk1ICo" : {
"balance" : 0,
"price_usd" : 0.0253829,
"symbol" : "2ND",
"time" : 1508769890908
}},
"3RD" : {
"-Kx8gN5FEZEPsOe8S-Tw" : {
"balance" : 0,
"price_usd" : 0.767354,
"symbol" : "3RD",
"time" : 1508769890908
}},
// for a total of 250 paths
}}
以这种方式对 push()
的每次调用都会导致到 Firebase 的往返。问题是 Firebase 只对前 100 个引用执行 push()
。
我在 Firebase 文档中找不到往返次数的限制:https://firebase.google.com/docs/database/usage/limits
我在 Whosebug
我的两个问题是:
Firebase 中是否有最大往返次数或是否存在其他错误?
如何通过将代码合并到多位置更新中来使我的代码正常工作?
使用不带参数的 push()
方法将在本地生成一个唯一的 ID,并且不会导致对数据库的网络操作:
If you provide a value to
push()
, the value will be written to the generated location. If you don't pass a value, nothing will be written to the Database and the child will remain empty (but you can use theReference
elsewhere).
因此,您可以使用它为需要推送到数据库的每个项目获取唯一 ID,将其存储在数组中,然后使用 update()
将所有项目同时发送到数据库多位置更新,例如:
storeTicker() {
const timestamp = firebase.database.ServerValue.TIMESTAMP;
var updates = {};
for (let object of market) {
const key = this.db.list('/Tickers').push().key; // Get a new unique key (locally, no network activity)
const path = object['symbol'] + '/' + key;
updates[path] = {symbol: object['symbol'], price_usd: object['price_usd'], balance: object['balance'], time: timestamp};
}
this.db.object('/Tickers').update(updates);
}
update()
method (similar to the official Firebase JavaScript SDK update()
method) 通过将整个数组发送到指定位置来工作:
The
values
argument contains multiple property-value pairs that will be written to the Database together. Each child property can either be a simple property (for example, "name") or a relative path (for example, "name/first") from the current location to the data to update.
有关这方面的文档示例,请参阅 updating data using AngularFire or update specific fields using the JavaScript SDK。