以下是你问题的答案:
你必须在某个地方配置Firebase FIRApp.configure().完成后,可以使用let firebaseDatabaseReference = FIRDatabase.database().reference()获取对该数据库的引用并开始使用它.问题不在于Firebase本身,而在于Swift的行为.
如果在AppDelegate func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
中放入FIRApp.configure()
,然后在MyDatabase class
中使用let firebaseDatabaseReference = FIRDatabase.database().reference()
outside个已声明函数sometimes,则代码FIRDatabase.database().reference()
在执行application didFinishLaunchingWithOptions
函数之前执行.
基本上,您的类正在try 获取对Firebase数据库before的引用,它有机会自行配置,并在控制台中生成错误"默认应用尚未配置"
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.
这就是为什么要移动FIRApp.configure()代码覆盖AppDelegate中的init(),本质上它确保在初始化AppDelegate时执行configure代码(在这种和大多数情况下,在初始化MyDatabase之前)
override init() {
super.init()
FIRApp.configure()
// not really needed unless you really need it FIRDatabase.database().persistenceEnabled = true
}
还要确保你超棒.init().