错误:`在使用Firestore之前先调用FirebaseApp.configure() [英] Error: `call FirebaseApp.configure() before using Firestore`
问题描述
背景信息
我已经开始为一个简单的App开发后端,并且已经建立了一个数据库类(名为 DBDelegate
),所有文件都将与之通信.在我的 AppDelegate.swift
中,我有以下内容:
静态公共变量dbDelegate:DBDelegate = DBDelegate()私人功能应用程序(_应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.LaunchOptionsKey:任何]?)->布尔{//应用程序启动后进行自定义的替代点.FirebaseApp.configure()返回真}
它是一个静态公共
,因此我可以从其他文件访问 dbDelegate
.
在其他文件中,我有以下几点可提高可读性:(因为它是一个类,因此将通过引用传递)
让dbDelegate = AppDelegate.dbDelegate
在我的 DBDelegate
类中:
var db = Firestore.firestore()在里面() {FirebaseApp.configure()}
构建和运行
构建代码时,它会正常运行.
在运行时,该应用会立即使用 SIGABRT
崩溃.错误消息是:由于未捕获的异常"FIRAppNotConfiguredException"而终止应用程序,原因:无法获取FirebaseApp实例.在使用Firestore之前,请先致电FirebaseApp.configure().
我尝试过的
DBDelegate
类的init函数上设置一个断点.它没有达到断点. dbDelegate
变量 lazy
: lazy不能在已经很懒的全局对象上使用
静态公共变量dbDelegate:DBDelegate!私人功能应用程序(_应用程序:UIApplication,didFinishLaunchingWithOptions launchOptions [UIApplication.LaunchOptionsKey:任何]?)->布尔{//应用程序启动后进行自定义的替代点.FirebaseApp.configure()dbDelegate = DBDelegate()返回真}
- 我收到编译错误:
静态成员'dbDelegate'不能用于类型为'AppDelegate'的实例
任何帮助都会很棒!
我找到了一个麻烦的解决方案,请参见下文.
首先,我要感谢@DionzB建议使用单例(我这样做了).我将在此答案中引用他/她的帖子.
好吧,所以经过一些研究并使用了断点,我发现我的自定义类实际上在AppDelegate之前执行.知道这一点后,我在以下行之前创建了一个变量:
static let shared = FirebaseService()
名称无关紧要,因为我/您不会调用它,并将其分配给 FirebaseApp.configure()
FirebaseService类变为:
Class FirebaseService:NSObject {让constantToNeverTouch = FirebaseApp.configure()静态让共享= FirebaseService()在里面() {}}
接下来,您必须确保FirebaseApp.configure()在代码中的其他位置都不存在.它也不应该在AppDelegate中.拥有多个FirebaseApp.configure()会使应用程序崩溃.
Background Info
I have started developing a backend for an simple App, and I have set up a database class (named DBDelegate
) that all the files will communicate with.
In my AppDelegate.swift
I have this:
static public var dbDelegate:DBDelegate = DBDelegate()
private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
FirebaseApp.configure()
return true
}
Its a static public
so I can access the dbDelegate
from other files.
In my other files, I have the following to help readability: (because it is a class it will pass by reference)
let dbDelegate = AppDelegate.dbDelegate
In my DBDelegate
class:
var db = Firestore.firestore()
init() {
FirebaseApp.configure()
}
Building and Running
When I build my code, it builds fine.
On run, the app promptly crashes with SIGABRT
.
The error message is:
Terminating app due to uncaught exception 'FIRAppNotConfiguredException', reason: 'Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore'
What I have tried
- I have tried putting a breakpoint on the init function in the
DBDelegate
class. It does not reach the breakpoint. - I have tried making the all the
dbDelegate
variableslazy
:- I got a compile error for the one in AppDelegate:
lazy must not be used on an already-lazy global
- Runtime errors for others: please call FirebaseApp.configure() before using Firestore.
- I got a compile error for the one in AppDelegate:
- I have tried the following (assigning dbDelegate in didFinishLaunchingWithOptions):
static public var dbDelegate:DBDelegate! private func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. FirebaseApp.configure() dbDelegate = DBDelegate() return true }
- I get Compile error:
Static member 'dbDelegate' cannot be used on instance of type 'AppDelegate'
Any help would be great!
Edit: I found a janky solution, see below.
Firstly, I would like to thank @DionzB for suggesting using singletons (which I did). I will reference his/her post in this answer.
Ok, so after some research and playing with breakpoints, I found that my custom class actually executes before the AppDelegate. Knowing such, I created a variable before the following line:
static let shared = FirebaseService()
the name does not matter, because I/you will not call it, and assign it to FirebaseApp.configure()
The FirebaseService class becomes:
class FirebaseService: NSObject {
let constantToNeverTouch = FirebaseApp.configure()
static let shared = FirebaseService()
init() {
}
}
Next, you must make sure that FirebaseApp.configure() is no where else in your code. It should not be in the AppDelegate either. Having multiple FirebaseApp.configure()'s crashes the app.
这篇关于错误:`在使用Firestore之前先调用FirebaseApp.configure()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!