Reputation: 133
I'm trying to enable offline data with my firebase database. Unfortunately when running this code, it tells me that 'Calls to setPersistenceEnabled must be made before any other usage of FIRDatabase instance.' I believe this is because firebase calls are asynchronous, but I can't figure out how to fix the issue.
from my AppDelegate:
//Firebase database
let myDatabase = Database.database().reference()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//Firebase
FirebaseApp.configure()
Database.database().isPersistenceEnabled = true
//check if user is already logged in
if Auth.auth().currentUser != nil {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
self.window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "signedIn")
} else {
// No user is signed in. Directs to Login/Register view.
}
return true
}
I've tried commenting out the check user login portion in case Auth.auth() was messing it up, but it didn't fix it. Should I be putting some of these things elsewhere instead of my AppDelegate?
Upvotes: 1
Views: 1914
Reputation: 1588
You are calling the database before initializing Firebase. Remove the let myDatabase from your AppDelegate and use them in your ViewControllers instead.
Your error is not related to persistence but is caused by calling a Firebase database before it is setup in your AppDelegate.
Upvotes: 1