0

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)
    }
}

Data Structure

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Lukas Bimba
  • 817
  • 14
  • 35
  • Please update your answer to say what the current code does that you don't want it to do, and include your data structure. – Jen Person Aug 25 '17 at 22:37
  • I added my data structure. Yet, the structure shows the these "URL" values exist. I need help to make my query for the "snapchatURL" work. Right now from what tests it isn't working. It print out my message "They added their social media" when I created a profile without that data – Lukas Bimba Aug 25 '17 at 22:59
  • If it doesn't have "snapchatURL" I want the button to be disabled otherwise if a user try's to tap the button the app will crash – Lukas Bimba Aug 25 '17 at 23:00

1 Answers1

3

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)
    }
Lukas Bimba
  • 817
  • 14
  • 35