简体   繁体   中英

How to push a list to Firebase Realtime Database

I'm developing a mobile application on Ionic 3. I have a notification object on firebase database. I'm pushing data for one record like this way:

fireNots = firebase.database().ref('/notifications');
addNotification(notReq: INotReq){
   this.fireNots.child(notReq.RecipientUId).push({
      Title: notReq.Title,
      Body: notReq.Body
      }).then(() => {
        resolve({ success: true });
      })     
}

And this is INotReq:

export interface INotReq{
    RecipientUId: string,
    Title: string,
    Body: string
}

My data structure on Firebase:

- notifications
- Q6cQqz0OVRPCq17OWb (RecipientUId)
- LtH7QZlWVUcIpNqb-O9
- Body:"You have a notification."
- Title:"Notification Title"

Now i need to push a list of notification (notReqs: INotReq[]).
Should i use a for loop like this?

  addMultipleNotification(notificationRequestArray: INotReq[]){    
    notificationRequestArray.forEach(notificationRequest => {
      this.addNotification(notificationRequest);
    });
  }

Would it be a bad practice? Or is there a better way to do this?

You have (at least) two other possibilities:

  1. Use the update() method which writes multiple values to the Database at once. See also here .

     addMultipleNotification(notificationRequestArray: INotReq[]){ const fireNots = firebase.database().ref('/notifications'); var updates = {}; notificationRequestArray.forEach(notificationRequest => { var newKey = fireNots.child(notificationRequest.RecipientUId).push().key; updates['/notifications/' + newKey] = { Title: notificationRequest.Title, Body: notificationRequest.Body }; }); return firebase.database().ref().update(updates).then(() => {...}) }
  2. Use Promise.all() that will run all the asynchronous push() operations in parallel and "returns a single Promise that fulfills when all of the promises passed as an iterable have been fulfilled":

     addMultipleNotification(notificationRequestArray: INotReq[]){ const fireNots = firebase.database().ref('/notifications'); var promises = []; notificationRequestArray.forEach(notificationRequest => { promises[fireNots.child(notificationRequest.RecipientUId).push({ Title: notificationRequest.Title, Body: notificationRequest.Body })] }); return Promise.all(promises).then(() => {...}) }

Note that there is an important difference between the two approaches:

  • By using update() the simultaneous updates are atomic: either all updates succeed or all updates fail.

  • On the other hand, if you use Promise.all() some of the pushes may fail (eg a security rule for a specific node prevents to write) but the other ones will be successful.


Also, note that the advantage with these two approaches is that you know exactly when all the writes to the database are done and therefore you can do whatever you want in the .then(() => {...}) method (inform the end-user, redirect to another page, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM