【发布时间】:2019-04-27 09:41:38
【问题描述】:
背景资料
我已经开始为一个简单的应用程序开发一个后端,并且我已经设置了一个所有文件都将与之通信的数据库类(名为DBDelegate)。
在我的AppDelegate.swift 我有这个:
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
}
它是static public,所以我可以从其他文件访问dbDelegate。
在我的其他文件中,我有以下内容来帮助提高可读性:(因为它是一个将通过引用传递的类)
let dbDelegate = AppDelegate.dbDelegate
在我的DBDelegate 班级:
var db = Firestore.firestore()
init() {
FirebaseApp.configure()
}
构建和运行
当我构建我的代码时,它构建得很好。
在运行时,应用程序会立即崩溃并显示 SIGABRT。
错误信息是:Terminating app due to uncaught exception 'FIRAppNotConfiguredException', reason: 'Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore'
我尝试过的
- 我尝试在
DBDelegate类的init 函数上设置断点。它没有到达断点。 - 我已尝试将所有
dbDelegate变量lazy:- 我在 AppDelegate 中遇到了编译错误:
lazy must not be used on an already-lazy global - 其他人的运行时错误:请在使用 Firestore 之前调用 FirebaseApp.configure()。
- 我在 AppDelegate 中遇到了编译错误:
- 我尝试了以下方法(在 didFinishLaunchingWithOptions 中分配 dbDelegate):
- 我得到编译错误:
Static member 'dbDelegate' cannot be used on instance of type 'AppDelegate'
任何帮助都会很棒!
编辑:我找到了一个 janky 解决方案,见下文。
【问题讨论】:
标签: ios swift firebase google-cloud-firestore