简体   繁体   English

使用iOS中的Firebase数据库检索数据

[英]Retrieving data with Firebase database in iOS

In my app I have a simple user base that looks like this: 在我的应用程序中,我有一个简单的用户群,如下所示:

在此输入图像描述

What I'm trying to do is to simply fetch this list once, to check wether a username is valid when a new user signs up with a new username. 我要做的是简单地获取此列表一次,以便在新用户使用新用户名注册时检查用户名是否有效。
The thing is that the only ways I found to retrieve data utilize some sort of observer methods, which are not good for me. 问题是,我发现检索数据的唯一方法是使用某种观察方法,这对我来说并不好。

The logic I'm trying to achieve (with the retrieving method that doesn't work) : 我想要实现的逻辑(使用不起作用的检索方法):

// When user tries to sign up with a new username

let username = nicknameField.text?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())    

self.usersRef.observeEventType(.Value) { (snapshot: FIRDataSnapshot) in
    let dict = snapshot.value as! NSDictionary
    for val in dict.allValues {
        if username == val as! String {
            // Present alert
            return
        }
    }
}   

self.usersRef.child(username).setValue(username) { (error, dbRef) in
    if error == nil {
        // Continue
    }
}

How can I simply just fetch the list of users once? 我怎样才能简单地获取一次用户列表? Thanks in advance! 提前致谢!

I had to change the observeEventType method to observeSignleEventOfType . 我不得不将observeEventType方法更改为observeSignleEventOfType
I have also updated my code to make it work (regardless): 我还更新了我的代码以使其工作(无论如何):

self.usersRef.observeSingleEventOfType(.Value) { (snapshot: FIRDataSnapshot) in
let dict = snapshot.value as! NSDictionary
for val in dict.allValues {
    if username == val as! String {
        // Present alert
        return
    }
    else {
        self.usersRef.child(username).setValue(username) { (error, dbRef) in
        if error == nil {
            // Continue
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM