How do I add data from a user in firebase rather than replace what is already there?

Logan Cohen

The app presents users with a random quote. I want users to be able to save the quotes and see which ones they saved. I can get a single quote to save, however, anytime the save button is clicked again, it overrides the previously saved quote with the new one. I've tried to find the answer elsewhere, but I cannot seem to get it to work. Below is my current code. I've also tried replacing setValue with updateChildValues, but I get the error Cannot convert value of type 'String' to expected argument type '[AnyHashable : Any]'.

import UIKit
import FirebaseDatabase
import FirebaseAuth

class QuotesViewController: UIViewController {

var ref: DatabaseReference?

override func viewDidLoad() {
    super.viewDidLoad()

    ref = Database.database().reference()
    
}

@IBAction func backToMain(_ sender: UIButton) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyboard.instantiateViewController(identifier: "mainHome")
        vc.modalPresentationStyle = .overFullScreen
        present(vc, animated: true)
}
@IBOutlet weak var quotesLabel: UILabel!
@IBAction func saveButton(_ sender: UIButton) {
    guard let user = Auth.auth().currentUser?.uid else { return }
    ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").child("quote").setValue(quotesLabel.text!)
    
}

@IBOutlet weak var nextButtonOutlet: UIButton!
@IBAction func nextQuoteButton(_ sender: UIButton) {
    
    let  quotesData = QuotesData()
    let randomQuote = quotesData.randomQuote()
    quotesLabel.text = randomQuote
      
}  
}

I've also tried:

ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").child("quote").updateChildValues(quotesLabel.text!)

Amr

That is expected, you basically are referencing the very same node child("quote") and trying to change its value. But if you want to have multiple quotes, what you need to do is to create multiple nodes under the parent child("Quotes") with different names.

One trivial way of doing so, you might append a different number to each new quote node, for example when you want to add a new quote, define the following path:

child("Quotes").child("quote1").setValue("...")

Path for another quote:

child("Quotes").child("quote2").setValue("...") And so on.

Alternatively, you can use Firebase Database reference method childByAutoId() to generate unique names. You will use that method after defining the parent node:

ref!.child("users").child(Auth.auth().currentUser!.uid).child("Quotes").childByAutoId().setValue(quotesLabel.text!)


Note:

Try to avoid force unwrapping as much as you can because that makes your app more prone to crashes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the user to input an int rather than a float?

How do I detect if a user is already logged in Firebase?

How do I create a clone of the following array rather than a reference?

How to get Exif data from an InputStream rather than a File?

How do I add text to a file without deleting what is already there?

How do I prevent the user from being able to add names that already exist?

Python - Tweepy - What do I need to add to make this code reply to an account rather than a keyword?

How do I add collections to authenticated user in firebase react js?

How do I check if the Selecteditems from Filedialog is already open by user?

R - How do I run a regression based on a correlation matrix rather than raw data?

How do I scroll within HTML rather than in the View?

How do I tell OSX to use matplotlib from brew, rather than default?

How do I less a filename rather than an inode number?

How can I return the name of a column rather than data it contains?

How do I create a mutation that pushes to an array rather than replacing it?

How do I get a TIFF bytestream from an OpenCV image, rather than a numpy array?

Swift - Why should I use viewDidAppear rather than viewWillAppear when redirect user if the user logged in already

What does "omitting directory" mean and how do I make it cp the directory rather than omit it

Swift Firebase How do I add a custom branch to my user?

How do I change the last layer in Keras to get logits rather than probabilities from model?

How do I get my tag to open from the center, rather than from the left?

How do I force browser to automatically retrieve fresh page from server rather than return cached page

How do I use Firebase Auth to show user-specific data from Firebase Database?

Firebase data not displaying in Recycler view. How do I display user specific data from firebase in recyclerview?

How do I add the name of the subjects rather than the index value?

How can i get data from a specific user rather then ID in my django app

How do I set the value of an <input> rather than the innerHTML of a <p>?

SplitViewController: How do I replace a the secondary view rather than pushing a new one?

How do i change this Powershell script to search by OU rather than user identity