Swift Firebase检查值是否存在 [英] Swift Firebase Check if Value Exists

查看:35
本文介绍了Swift Firebase检查值是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查用户内部是否存在值.我基本上是在创建检查是否要激活或禁用按钮的值是否存在.我有一个关注/取消关注系统,但问题是,如果一个用户尚未添加某些值并且用户想要关注它们,则该应用程序将崩溃.因此,我只想取消激活按钮,直到用户添加值,然后这些值才使按钮处于活动状态.我一直在尝试下面的代码,但是没有运气.我想检查他们是否创建了一个值"snapchatURL",它是否包含一个URL无关紧要,我只需要知道该值是否存在URL就可以了.

I am trying to check if a value exists inside a user. I am creating basically a check to activate or deactivate a button in regards if the value exists. I have a follow/unfollow system but the problem is that if one user has not yet added certain values and a user wants to follow them, the app will crash. So I just want to deactivate the button until the user adds the values which would then make the button active. I have been giving this code below a try but no luck. I want to check if they created a value "snapchatURL", it doesn't matter if it holds a URL or not, I only need to know if that value exists with or without the URL.

databaseRef.child("Businesses").child(self.otherUser?["uid"] as! String).queryOrdered(byChild: "snapchatURL").observe(.value, with: { (snapshot) in
        if(snapshot.exists()) {
            self.followButton.isEnabled = true
            print("They added their social media")
        } else {
            self.followButton.isEnabled = false
            print("They did not add their social media")
        }
    }) { (error) in
        print(error.localizedDescription)
    }


@IBAction func didTapFollow(_ sender: Any) {

    let followersRef = "followers/\(self.otherUser?["uid"] as! String)/\(self.loggedInUserData?["uid"] as! String)"

    let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String)


    if(self.followButton.titleLabel?.text == "Favorite") {
        print("follow user")

        let followersData = ["email":self.loggedInUserData?["email"] as! String]
        let followingData = ["businessName":self.otherUser?["businessName"] as! String, "businessStreet":self.otherUser?["businessStreet"] as! String, "businessCity":self.otherUser?["businessCity"] as! String, "businessState":self.otherUser?["businessState"] as! String, "businessZIP":self.otherUser?["businessZIP"] as! String, "businessPhone":self.otherUser?["businessPhone"] as! String, "businessWebsite":self.otherUser?["businessWebsite"] as! String,"businessLatitude":self.otherUser?["businessLatitude"] as! String, "businessLongitude":self.otherUser?["businessLongitude"] as! String, "facebookURL":self.otherUser?["facebookURL"] as! String, "twitterURL":self.otherUser?["twitterURL"] as! String, "instagramURL":self.otherUser?["instagramURL"] as! String, "googleURL":self.otherUser?["googleURL"] as! String, "yelpURL":self.otherUser?["yelpURL"] as! String, "foursquareURL":self.otherUser?["foursquareURL"] as! String, "snapchatURL":self.otherUser?["snapchatURL"] as! String]


        let childUpdates = [followersRef:followersData, followingRef:followingData]
        databaseRef.updateChildValues(childUpdates)

        print("data updated")

    } else {

        let followersRef = "followers/\(self.otherUser?["uid"] as! String)/\(self.loggedInUserData?["uid"] as! String)"
        let followingRef = "following/" + (self.loggedInUserData?["uid"] as! String) + "/" + (self.otherUser?["uid"] as! String)

        let childUpdates = [followingRef:NSNull(),followersRef:NSNull()]
        databaseRef.updateChildValues(childUpdates)
    }
}

推荐答案

我明白了.不必进行任何类型的查询,只需一个简单的".child":)

I got it figured out. Didn't have to do a query of any kind, just a simple ".child" :)

databaseRef.child("Businesses").child(self.otherUser?["uid"] as! String).child("snapchatURL").observe(.value, with: { (snapshot) in
        if(snapshot.exists()) {
            self.followButton.isEnabled = true
            print("They added their social media")
        } else {
            self.followButton.isEnabled = false
            print("They did not add their social media")
        }
    }) { (error) in
        print(error.localizedDescription)
    }

这篇关于Swift Firebase检查值是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆