2017-08-25 147 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) 
    } 
相關問題