1

In Firebase 3.0+ the method to get the database reference was as such:

var ref: FIRDatabaseReference!

ref = FIRDatabase.database().reference()


ref.child("rooms").observeSingleEventOfType(.Value, withBlock: { (snapshot) in

    if snapshot.hasChild("room1"){

        print("true rooms exist")

    }else{

        print("false room doesn't exist")
    }

})

In Firebase 4.0 the naming convention changed but I can't get it to work!?

var ref: DatabaseReference!    
ref = Database.database().reference()

Has anyone else encountered this?

The swift compiler is suggesting removing the () from the lowercase database function call

if you remove it, it then throws the error message:

  • Database has no member reference

So how do you get the reference then!?

Pod file:

pod 'Firebase/Core'
pod 'Firebase/Database'
iosforme
  • 25
  • 1
  • 10

4 Answers4

1

You need to use the update code, perhaps the compiler hasn't done a good job of suggesting a fix

var ref: DatabaseReference!

ref = Database.database().reference()  

ref.child("rooms").observeSingleEvent(of: .value, with: { (snapshot) in

  // ...

  })

as you can see, the code works perfectly, consider pod deintegrate & pod update

enter image description here

Mentos
  • 483
  • 1
  • 4
  • 14
  • its failing at : ref = FIRDatabase.database().reference() I makes no difference what the rest of the code does when its fails at setting the init reference!? – iosforme May 19 '17 at 20:48
  • did you declare `ref` before? – Mentos May 19 '17 at 20:49
  • @iosforme, what error are you hitting?, try using my above code and see if it would work, besides your implementation... just to narrow the issue down – Mentos May 19 '17 at 20:54
  • its in the question, first it suggests to remove the () when u do it says that database has no member called reference – iosforme May 19 '17 at 20:56
  • var ref: DatabaseReference! ref = Database.database().reference() – iosforme May 19 '17 at 20:57
  • the FIR... naming convention has been removed, i was putting that as it worked prior to 4.0 release – iosforme May 19 '17 at 20:58
  • @iosforme, hmmm, everything seems in order to be honest... I went through the code I use and the documentation and everything seems alright. Try `pod deintegrate`, `pod install` – Mentos May 19 '17 at 21:09
  • I ran the pod reintegrate and install and the same thing occurred – iosforme May 19 '17 at 21:28
  • this is nuts... I have the same thing as you and its erring out on me!? I ran the pod commands you provided as well started a new project instance and the same error occurs !? WTF!!!! – iosforme May 19 '17 at 21:40
  • @iosforme did you try deleting the derived Data folder? – Mentos May 19 '17 at 21:41
  • i ran this: rm -rf ~/Library/Caches/CocoaPods; rm -rf Pods; rm -rf ~/Library/Developer/Xcode/DerivedData/*; pod deintegrate; pod setup; pod install; – iosforme May 19 '17 at 21:51
1

The firebase documentation needs to be updated. 1. import both of these at the top of your class

import FirebaseCore

import FirebaseDatabase
  1. Getting a DB reference only works within ViewDidLoad()

override func viewDidLoad() {

super.viewDidLoad()
var ref: DatabaseReference!
ref = Database.database().reference()
}
0

Your syntax seems right to me (for the new Firebase 4):

var ref: DatabaseReference!    
ref = Database.database().reference()

And, looking at their source code (FIRDatabase.h), everything you are doing matches perfectly:

FIR_SWIFT_NAME(Database)
@interface FIRDatabase : NSObject

+ (FIRDatabase *) database FIR_SWIFT_NAME(database());

- (FIRDatabaseReference *) reference;

Are you sure you are on their latest SDK version? As a last resort, try this:

pod cache clean
rm Podfile.lock
rm -rf Pods
pod install
rm -rf DerivedData
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
0

For Firebase 4.0, there is a change

  • Removing the FIR prefix across names for all constants, protocols, classes, enums, and type definitions.
  • Renaming FIRApp to FirebaseApp.
  • Renaming FIROptions to FirebaseOptions.

Link: https://firebase.google.com/docs/reference/ios/naming-migration-guide

Mike Yang
  • 2,581
  • 3
  • 24
  • 27