2017-08-25 163 views
0

我正在尝试检查用户内是否存在值。如果值存在,我基本上会创建一个检查来激活或停用按钮。我有一个关注/取消关注系统,但问题是,如果一个用户还没有添加特定的值,并且用户想要关注他们,那么该应用会崩溃。所以我只想停用这个按钮,直到用户添加可以使按钮激活的值。我一直在尝试下这个代码,但没有运气。我想检查他们是否创建了值“snapchatURL”,它是否包含URL并不重要,我只需要知道该值是否存在URL或不存在。Swift Firebase检查值是否存在

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

+0

请更新你的答案说什么当前的代码做,你不希望它做的,包括你的数据结构。 –

+0

我添加了我的数据结构。然而,结构显示存在这些“URL”值。我需要帮助才能对“snapchatURL”工作进行查询。现在从什么测试它不起作用。当我创建没有该数据的个人资料时,它会打印出我的消息“他们添加了他们的社交媒体” –

+0

如果它没有“snapchatURL”,我希望按钮被禁用,否则如果用户尝试点击按钮,应用程序会崩溃 –

回答

1

我想通弄明白了。没做任何类型的查询,只是一个简单的“.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) 
    }