0

I wanted to try following some online tutorials for making apps connecting to Firebase, but they are outdated as they are still using FIRApp.configure(), etc. Therefore I have to change the code correspondingly and not able to have a complete workable version of such app.

By now, I have already:

  1. Read through the documentation from Firebase

  2. How to define the "ref" variable in AppDelegate (How to fix Firebase SIGABRT crash iOS),

  3. How to set that variable in AppDelegate to be a global variable and readable in other ViewControllers (How do I get a reference to the app delegate in Swift?),

  4. Checked if I have included the correct pod files, and set the value of the variable inside viewDidLoad section (Where do I add the database reference from Firebase on iOS?),

  5. Noticed that pod files might need to be updated, performed pod disintegrate and pod update (Get a database reference in the new Firebase 4.0 release).

For podfile, I have included:

pod 'Firebase/Core'
pod 'Firebase/Database'

For AppDelegate, (parts not shown are as default)

import UIKit
import CoreData
import Firebase
import FirebaseDatabase //tried not having this line, still the same

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var ref: DatabaseReference!

func application(_application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    FirebaseApp.configure()
    self.ref = Database.database().reference()
    return true
}

For ViewController, (parts not shown are as default or either certainly not related)

import UIKit
import Firebase
import FirebaseDatabase //again, tried with/without this line

class ViewController: UIViewController {

    @IBOutlet weak var emailfield: UITextField!
    @IBOutlet weak var usrnamefield: UITextField!
    @IBOutlet weak var pwdfield: UITextField!
    @IBOutlet weak var loginbtn: UIButton!
    @IBOutlet weak var createnewaccbtn: UIButton!
    @IBOutlet weak var forgetpwdbtn: UIButton!
    //......
    @IBAction func createnewAcc(_ sender: UIButton) {
        let username = usrnamefield.text
        let email = emailfield.text
        let password = pwdfield.text

        if username != "" && email != "" && password != "" {
            let appDelegate = UIApplication.shared.delegate as! AppDelegate
            appDelegate.ref.child("users").setValue(["username": username]) //this line is highlighted with the error below
            //Cannot even update one single field to the database
        }
    }
}

But when I run the Simulator, the error says

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Reference:

Xcode 9.2, CocoaPods 1.4.0, Deployment Target: 9.2, 10.3

Update (2018 Feb 27, GMT 18:00):

Tried defining databaseReference variable in the ViewController directly, and assigned value using databaseReference = Database.database().reference() inside viewDidLoad.

It turns out to receive signal SIGABRT in AppDelegate (class AppDelegate line). Disabling the above value assignment code will solve the signal SIGABRT problem, but still cannot connect to Firebase Database...

ryuenkc
  • 9
  • 4
  • Did you add the file "GoogleService-Info.plist" in your project? – Renan Silveira Feb 26 '18 at 19:43
  • As far as I can see the only optional being unwrapped on that line (`appDelegate.ref.child("users").setValue(["username": username])`) is `username`. Can you try if it works with a hard-coded value, e.g.: `appDelegate.ref.child("users").setValue(["username": "ryuenkc"])` – Frank van Puffelen Feb 26 '18 at 20:28
  • Renan, I have the plist file in the project already, ensured correct bundle ID. – ryuenkc Feb 27 '18 at 15:31
  • Frank, I will try that as well, thanks! But the point that it marked as fatal error, I think is about the "ref" part...? – ryuenkc Feb 27 '18 at 15:32
  • Frank, tried using hard-coded value, still doesn't work. – ryuenkc Feb 27 '18 at 17:35
  • Hi ryuenkc, You can access `Database.database().reference()` from anywhere without having to keep a reference to it in the AppDelegate, it will always get a reference to the root of your Database. Since that's the case, inside your `if` statement remove the `let appDelegate = ...` call and just use `var ref = Database.database().reference()` and use that in the line below. – ryanwils Mar 01 '18 at 11:53
  • Thanks Ryan, but I have tried directly assigning the variable inside the ViewController.swift, but seems still the same situation...probably I should try with a plain project first... – ryuenkc Mar 01 '18 at 15:25

1 Answers1

0

Well what you shown in code is writing in firebase not retrieving. Now the main point, if you want to write then use first some constant value if works mean firebase connected correctly otherwise text fields or creating database ref issue. Is there any reason, not giving reference in same ViewController where you wanna read or write in firebase?.

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
  • I thought I had seen someone saying that the database reference can only be called/loaded inside AppDelegate? Or maybe I have misunderstood the meaning... Good suggestion to change to constant values...will try when I am back to my computer a bit later, thanks! – ryuenkc Feb 27 '18 at 15:36
  • Tried hard-coded strings and still no luck. Narrowed down to not able to create the database reference, even defining the variable needed in the `ViewController` and assigning value in `viewDidLoad`... – ryuenkc Feb 27 '18 at 18:09
  • then your app is not properly connected with firebase. tell me how did you connection? – MRizwan33 Feb 27 '18 at 20:10
  • I am connecting as what the documentation says... If you are adking about my internet connection, it is using WiFi via router...though that should not matters I suppose lol – ryuenkc Mar 01 '18 at 15:23
  • well im not asking about internet connection.may be you missed any step from documentation. – MRizwan33 Mar 01 '18 at 15:27
  • Haha, I had read the documentation for at least three times and I think I did even more than the documentation has mentioned, but still... – ryuenkc Mar 02 '18 at 18:07