xcode 从新的 Firebase 检索数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37316179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve data from new Firebase
提问by Dmitry Kuzin
Please help. After migrating to new Firebase I can't retrieve data. Use this construction:
请帮忙。迁移到新 Firebase 后,我无法检索数据。使用这个结构:
let ref = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
ref.observeEventType(FIRDataEventType.Value, withBlock: { (snapshot) in
let postDict = snapshot.value as! [String : AnyObject]
print("\(postDict)")
})
}
After running I see error:
运行后我看到错误:
2016-05-19 10:04:22.264 123a[88652:13688922] The default app has not been configured yet.
2016-05-19 10:04:22.276 123a[88652:13688922] *** Terminating app due to uncaught exception 'MissingDatabaseURL', reason: 'Failed to get FIRDatabase instance: FIRApp object has no databaseURL in its FirebaseOptions object.'
*** First throw call stack:
I read documentation, but can't resolve this problem. GoogleService-Info.plist I add to project.
我阅读了文档,但无法解决此问题。我添加到项目中的 GoogleService-Info.plist。
回答by Tom Wolters
I didn't see this answer yet, I had to add the configure call to the AppDelegate init method. So it looks like:
我还没有看到这个答案,我不得不将配置调用添加到 AppDelegate init 方法。所以它看起来像:
override init() {
super.init()
// Firebase Init
FIRApp.configure()
}
回答by Ugur
Had the same problem. I looked for linking problems that are related to the plist but that wasn't the problem. I thought maybe it has caused because of that my initial view controller is revoked before the configurations are completed. I solved the problem by experimenting a bit.
有同样的问题。我寻找与 plist 相关的链接问题,但这不是问题。我想可能是因为我的初始视图控制器在配置完成之前被撤销了。我通过一些实验解决了这个问题。
My initial view controller was this:
我最初的视图控制器是这样的:
let ref = FIRDatabase.database().reference()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
I've changed that to this:
我把它改成这样:
var ref = FIRDatabaseReference.init()
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
// Do any additional setup after loading the view, typically from a nib.
}
Crash resolved.
崩溃解决。
回答by ColdLogic
So, with mine, I also had a ref being declared immediately when the view controller was instantiated. I had to make it load after the app had been configured in the app delegate with FIRApp.configure()
.
所以,对于我的,当视图控制器被实例化时,我也立即声明了一个 ref。在应用程序委托中使用FIRApp.configure()
.
Before:
前:
let serverRef = Firebase("firebaseURL")
After:
后:
lazy var serverRef = FIRDatabase.database().reference()
This delays the instantiation of the database reference until its needed, which wont be until viewDidLoad
on your initial view controller.
这将数据库引用的实例化延迟到需要时,直到viewDidLoad
您的初始视图控制器上才需要。
回答by JiuJitsuCoder
To build on the answer given by @ColdLogic, the reason I had this error was because I had my Firebase database reference being created in an init method on a view controller, notin the viewDidLoad method. Since the init methods for all classes that are instantiated when the app launches are called beforethe application:DidFinishLaunchingWithOptions method in the AppDelegate, it was causing this crash. Moving this line of code:
为了建立在@ColdLogic 给出的答案的基础上,我出现此错误的原因是我的 Firebase 数据库引用是在视图控制器的 init 方法中创建的,而不是在 viewDidLoad 方法中。由于在应用程序启动时实例化的所有类的 init 方法是在 AppDelegate 中的 application:DidFinishLaunchingWithOptions 方法之前调用的,因此导致了此崩溃。移动这行代码:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
firebaseRef = FIRDatabase.database().reference()
}
override func viewDidLoad() {
...
}
}
to here:
到这里:
class MyViewController {
var firebaseRef: FIRDatabaseReference
required init?(coder aDecoder: NSCoder) {
...
}
override func viewDidLoad() {
...
self.firebaseRef = FIRDatabase.database().reference()
}
}
solved the problem for me.
为我解决了这个问题。
回答by Markinson
I too had a problem with the Firebase Database. Fixed it by adding
我也遇到了 Firebase 数据库的问题。通过添加修复它
import FirebaseDatabase
to my code
到我的代码
回答by enc_life
I was getting this error until I made FIRApp.configure()
the first line in the AppDelegate
didFinishLaunchingWithOptions
我收到此错误,直到我FIRApp.configure()
在AppDelegate
didFinishLaunchingWithOptions
回答by Tiago Almeida
In my case I had to change the configure to be called before calling the super applicationDidLaunch:
就我而言,我必须在调用超级 applicationDidLaunch 之前更改要调用的配置:
[FIRApp configure];
[super application:application didFinishLaunchingWithOptions:launchOptions];
回答by Jan Hartwig
Had the same problem today, you need the "firebase_url": "https://xxxxxxxxxx.firebaseio.com" at google-services.json and for that do this steps https://support.google.com/firebase/answer/7015592#androidIf you had one file from google cloud platform before, maybe there are some differences and you have to check. For me this works.
今天遇到了同样的问题,您需要在 google-services.json 上使用“firebase_url”:“ https://xxxxxxxxxx.firebaseio.com”,为此请执行以下步骤https://support.google.com/firebase/answer/ 7015592#android如果你之前有一个来自谷歌云平台的文件,可能有一些差异,你必须检查一下。对我来说这是有效的。
回答by Junaid Mukhtar
Make sure you have downloaded the GoogleService-Info.plist
file from your Firebase console and added to the root of your project directory.
确保您GoogleService-Info.plist
已从 Firebase 控制台下载该文件并将其添加到项目目录的根目录中。
Once you have added it call this function from didFinishLaunchingWithOptions
in AppDelegate:
添加后,它会从didFinishLaunchingWithOptions
AppDelegate 中调用此函数:
FIRApp.configure()
Thats it, it should be up and running!
就是这样,它应该启动并运行!