The default app has not been configured yet - ios

I'm trying to upgrade my app to the new version of Firebase. I went through the setup guide and edited all of my code to match the new syntax. However, when I run the app, I get these two errors.
The default app has not been configured yet.
Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'
I have FIRApp.configure() in the AppDelegate and I have the GoogleServices-Info.plist imported into my project. The plist has all of the correct info as well. Anyone else running into this or know how to fix it?

Here's the answer to your problem:
To configure Firebase you have to execute FIRApp.configure() somewhere. After this is done you can use let firebaseDatabaseReference = FIRDatabase.database().reference() to get a reference to that database and start using it. The problem isn't with Firebase "per se" but with how Swift behaves.
If you put FIRApp.configure() in your AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool and then in the MyDatabase class you use let firebaseDatabaseReference = FIRDatabase.database().reference() outside of your declared functions sometimes the code FIRDatabase.database().reference() executes before the application didFinishLaunchingWithOptions function is executed.
Essentially your class is trying to get a reference to the Firebase database before it has a chance to configure itself, generating the error in the console "The default app has not been configured yet."
Note: This doesn't happen all the time, sometimes the application is slow to start, in iOS Simulator for example, and it doesn't have a chance to finish before MyDatabase "let" executes and tries to get a reference.
That is why moving the FIRApp.configure() code to override init() in AppDelegate works, essentially it makes sure the configure code gets executed when AppDelegate is initialised (in this and most cases, before MyDatabase is initialised)
override init() {
super.init()
FIRApp.configure()
// not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}
Also make sure you super.init() (so you super classes get the "message") so you override doesn't do more harm than good.

I'm also using Fabric and in my case it was the order of Fabric and Firebase initializations. I had to initialize Firebase first.
So changing
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Fabric.with([Crashlytics.self])
FirebaseApp.configure()
...
}
to:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
Fabric.with([Crashlytics.self])
...
}
fixed the problem.

In AppDelegate.m, outside of didFinishLaunchingWithOptions,
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}

Make sure you are having DATABASE_URL key in your GoogleService-Info.plist

Swift 5 - Easy Solution
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
//MARK:- This function will auto run and firebase will configure successfully
override init() {
super.init()
FirebaseApp.configure()
// not really needed unless you really need it
FIRDatabase.database().persistenceEnabled = true
}
Happy Coding

iOS 9.2
Swift 2.1.1
Xcode 7.2.1
Mac OSX 10.10.5
Same error here using the following code:
AppDelegate.swift:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FIRApp.configure()
return true
}
ViewController.swift:
import UIKit
import Firebase
class ViewController: UIViewController {
var db = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Create some data in Firebase db:
db.child("key").child("subkey").setValue("hello world")
}
I also added the file GoogleService-Info.plist to my project directory as described in the Firebase Getting Started Guide.
And I made my Firebase db public with the following Rules:
{
"rules": {
".read": true,
".write": true
}
}
Making the following changes to ViewController.swift is what worked for me:
class ViewController: UIViewController {
var db: FIRDatabaseReference!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
db = FIRDatabase.database().reference()
db.child("key").child("subkey").setValue("hello world")
}
Prior to running my app, my Firebase db looked like this:
myiosdata-abc123: null
After running my app, my Firebase db looked like this:
myiosdata-abc123
- key
|
+--- subkey: “hello world”

I had several normal working projects with FIRApp.configure () code in AppDelegate.swift:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
return true
}
Everything worked great for quite some time, but yesterday and today I opened a Swift 3 project inside my Xcode 7.3.1 (I am still working in Swift 2, but opened Swift 3 project to see what is changed), and suddenly in all my Swift 2 apps and projects that I am still working on, got the same error:
The default app has not been configured yet
Every project now when I open in XCode, getting same error, I didn't know what to do, but after implementing #MichaelWilliams' code, everything works fine again.
I have debug my Xcode (clear and reload console), but nothing works beside this new approach by Michael.
This one resolved my problem:
override init() {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
}
Can this new code somehow make my app's unstable and can I be afraid to see problems with connecting/working with Firebase database now?

Try re-download GoogleService-Info.plist from your console and add it to your project, That worked for me!

If you are using Xcode 9, Swift 4 and Firebase 4 please do the following:
override init() {
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
}

The cleanest solution to me here is to have lazy properties in case you want to have the db on top of your file. So let's say you have a FirebaseService class where you want to have Firestore.firestore() db constant to use it in all of the functions in that class:
private lazy var db = Firestore.firestore()
Or if you are using Firebase Storage:
private lazy var storage = Storage.storage().reference()
Also keep in mind that if you are referencing the Database/Storage in init() of your classes, you still might have the same problem so avoid that.

Related

xmppStreamDidConnect never gets called in Swift

I am trying to connect to my chat server in a Swift app using XMPPFramework, but the didConnect delegate method never gets called.
I have created a basic app in Objective C and I can connect and authenticate in my chat sever without problems.
In the Swift project I tried to connect with the code:
class AppDelegate: UIResponder, UIApplicationDelegate {
var stream:XMPPStream = XMPPStream()
var reconnect:XMPPReconnect = XMPPReconnect()
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
stream.addDelegate(self, delegateQueue: DispatchQueue.main)
stream.myJID = XMPPJID(string: "user#chatserver.net")
reconnect.activate(stream)
do {
try stream.connect(withTimeout: XMPPStreamTimeoutNone)
}
catch let err{
print("error occured in connecting\(String(describing: err.localizedDescription))")
}
return true
}
I’ve debugged XMPPFramework and in the method - (void)handleStreamFeatures a call to the delegate is executed :
[multicastDelegate xmppStreamDidConnect:self];
I’ve watched the multicastDelegateObject and has a node with reference to my delegate, and to OS_dispatch_queue_main, but after execution my xmppStreamDidConnect method isn’t executed.
As it is described in this Github Issue, the problem was the method declaration.
My xmppStreamDidConnect need an underscore, the root problem was that if you don't import the swift extensions the compiler marks that declaration as incorrect, although it works.
So to fix my problem, I need to import the pod 'XMPPFramework/Swift' and change the method declaration to
func xmppStreamDidConnect(_ sender: XMPPStream) {

Realm in appdelegate.swift not working on other files

This is a simple one probably but am I doing something wrong here :
I have this in my appdelegate.swift :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//configure Firebase
FirebaseApp.configure()
let realm = try! Realm()
return true
}
then I want to use refer to it in my view controller files like this :
do {
try realm.write {
realm.add(workoutData)
}
} catch {
print("Sorry no good")
}
but it says "use of unresolved identifier 'realm'".
But I thought the point of putting in app delegate was so you didn't need to do the 'let realm = try! Realm()' on every single view controller?
I have a firebase instance which is configured in appdelegate by doing :
FirebaseApp.configure()
and that works fine.
Am I missing something here? I can't find another answer that covers this so basically (there are some relating to migrations etc) so I'm assuming this is something really simple!
David Pastor answered this for me..
For any noobs that come this way....
You shouldn't declare it as a global variable anyway because that leads to issues as your app scales and if you did then the way i've done it wouldn't be right anyway!

Swift crashes - SIGABRT - Also how to track crashes

I am completely new to Swift, Xcode and programming on Mac.
I have started working on some kind of app, and all I really did was add Firebase (Which worked), and then try to add Google Sign In. Nothing else exists really. So There really was a Google Sign In Button. I think the crash started happening when I connected a property to the Google Sign In Button.
Alas, I am new to this, and I don't even know how to debug properly. Can someone please be of assistant ?
My code :
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
FirebaseApp.configure()
GIDSignIn.sharedInstance().clientID =
FirebaseApp.app()?.options.clientID
GIDSignIn.sharedInstance().delegate = self
return true
}
override func viewDidLoad()
{
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
}
This is the property:
#IBOutlet weak var googleLoginButton: GIDSignInButton!
And then try to run your project so you get that line where you app crashes

Firebase: FIRApp.configure() keeps wanting me to change it to FirebaseApp.configure()

So I made a project that included lots of extra code so I could try out different things, and I am slowly copy-pasting the files from textedit into my new project.
I created a new Firebase account, added the google plist file, initialized and installed the pods needed (also changed the iOS version to 10.0 on the pod file)... yet it's still wanting me to use FirebaseApp.configure() instead of FIRApp.configure().
Should I just delete the whole thing and try again? I just created a new Xcode project, so if I had to delete it right now that would be fine.
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
override init() {
super.init()
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
return true
}
There hasn't been much code written, but I can't figure out what could have made this happen... especially since I just started the new project.
FirebaseApp.configure() is correct. I checked out the documentation just now and FIRApp.configure() has been renamed FirebaseApp.configure(). I also ran pod update on one of my projects just to confirm, and my project had me change to using FirebaseApp.configure(). So no need to rebuild!
I can confirm that FIRApp.configure() can be changed to FirebaseApp.configure()
In firebase it suggest to add the following to app delegate:
"
import UIKit import Firebase
#UIApplicationMain class AppDelegate: UIResponder,
UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
-> Bool {
FirebaseApp.configure()
return true } }
"
This includes the FirebaseApp.configure() command

iOS Firebase app not configured after calling FIRApp.configure()

I have an iOS (Swift) app with the following code in the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FIRApp.configure()
FIRDatabase.database().persistenceEnabled = true
return true
}
The app crashes on the persistenceEnabled = true line, with exception FIRAppNotConfigured, and the message "Failed to get default FIRDatabase instance. Must call FIRApp.configure() before using FIRDatabase."
Obviously, I've called FIRApp.configure() immediately before this, so the suggested solution is incorrect. The log output even shows "Configuring the default app" when that is called.
What might the problem be, and how could I resolve it so I can use the FIRDatabase?
Try:
override init() {
// Firebase Init
FIRApp.configure()
}

Resources