Retrieve data from new Firebase

前端 未结 9 2821
醉梦人生
醉梦人生 2021-02-15 23:40

Please help. After migrating to new Firebase I can\'t retrieve data. Use this construction:

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

override func viewDidL         


        
相关标签:
9条回答
  • 2021-02-16 00:22

    So, with mine, I also had a ref being declared immediately when the view controller was instantiated. I had to make it load after the app had been configured in the app delegate with FIRApp.configure().

    Before:

    let serverRef = Firebase("firebaseURL")
    

    After:

    lazy var serverRef = FIRDatabase.database().reference()
    

    This delays the instantiation of the database reference until its needed, which wont be until viewDidLoad on your initial view controller.

    0 讨论(0)
  • 2021-02-16 00:22

    In my case I had to change the configure to be called before calling the super applicationDidLaunch:

    [FIRApp configure];
    
    [super application:application didFinishLaunchingWithOptions:launchOptions];
    
    0 讨论(0)
  • 2021-02-16 00:23

    I too had a problem with the Firebase Database. Fixed it by adding

    import FirebaseDatabase
    

    to my code

    0 讨论(0)
  • 2021-02-16 00:30

    To build on the answer given by @ColdLogic, the reason I had this error was because I had my Firebase database reference being created in an init method on a view controller, not in the viewDidLoad method. Since the init methods for all classes that are instantiated when the app launches are called before the application:DidFinishLaunchingWithOptions method in the AppDelegate, it was causing this crash. Moving this line of code:

    class MyViewController {
    
       var firebaseRef: FIRDatabaseReference
    
       required init?(coder aDecoder: NSCoder) {
          ...
          firebaseRef = FIRDatabase.database().reference()
       }
    
       override func viewDidLoad() {
          ...
       }
    }
    

    to here:

    class MyViewController {
    
       var firebaseRef: FIRDatabaseReference
    
       required init?(coder aDecoder: NSCoder) {
          ...
       }
    
       override func viewDidLoad() {
          ...
          self.firebaseRef = FIRDatabase.database().reference()
       }
    }
    

    solved the problem for me.

    0 讨论(0)
  • 2021-02-16 00:31

    Had the same problem. I looked for linking problems that are related to the plist but that wasn't the problem. I thought maybe it has caused because of that my initial view controller is revoked before the configurations are completed. I solved the problem by experimenting a bit.

    My initial view controller was this:

        let ref = FIRDatabase.database().reference()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    }
    

    I've changed that to this:

        var ref = FIRDatabaseReference.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ref = FIRDatabase.database().reference()
    
        // Do any additional setup after loading the view, typically from a nib.
    
    }
    

    Crash resolved.

    0 讨论(0)
  • 2021-02-16 00:32

    I didn't see this answer yet, I had to add the configure call to the AppDelegate init method. So it looks like:

    override init() {
        super.init()
        // Firebase Init
        FIRApp.configure()
    }
    
    0 讨论(0)
提交回复
热议问题