Adam Zarn
Adam Zarn

Reputation: 2100

Using Firebase push() to create a list

Here is the reference I created to my Firebase database

let ref = FIRDatabase.database().reference()

However, when I try to use this as the base_url for a push(), I get this error:

Value of type 'FIRDatabaseReference' has no member 'push'

I tried to change my base_url to this:

let ref = Firebase.database().ref()

But then I get

Module 'Firebase' has no member 'database'

Here's my Podfile. Am I missing something? I have imported Firebase at the top of my file.

# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'

target 'ValleybrookMessenger' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!

# Pods for ValleybrookMessenger

    pod 'Firebase'
    pod 'Firebase/Database'
    pod 'Firebase/Auth'

  target 'ValleybrookMessengerTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'ValleybrookMessengerUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

Upvotes: 1

Views: 507

Answers (2)

Prits Ramani
Prits Ramani

Reputation: 100

Swift doesn't provide push method as javascript does, but you can perform similar action by following code

let ref = Database.database().reference("Items")
var key = ref.childByAutoId().key as? String ?? ""
ref.child(key).setValue(["Item_Name" : name])

In javascript push method automatically generates a unique key but in swift you can generate by

var key = ref.childByAutoId().key as? String ?? ""

which you can add to reference as a child and setValue inside that key.

Upvotes: 0

mjr
mjr

Reputation: 807

Swift doesn't have a push() method, so the equivalent you probably want is childByAutoId().

The first way you created the reference is correct:

let ref = FIRDatabase.database().reference()

Upvotes: 3

Related Questions