swift - Swift 和 Firebase 关注/取消关注按钮
问题描述
我目前正在使用firebase构建一个应用程序,我有一个关注按钮,可以关注用户或取消关注用户(如果已经关注)。
在我无法关注用户之后,当我单击按钮时,数据会添加到 Firebase 中,然后再次被删除..
当我再次运行该应用程序时,除了相同的错误之外,当我再次关注时,一切都可以完美运行,这是我的 firebase 数据库 [FirebaseDatabase][1]
这是我的代码
var follower = false
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
self.tableView.addSubview(refreshControl)
if guest == true {
buttonRight.setImage(UIImage(named:"icons8-user-menu-male-filled-30"), for: .normal)
firebaseControllerHandle.delegate = self
firebaseControllerHandle.getUserInfo(uid:guestuid)
firebaseControllerHandle.userDelegate = self
firebaseControllerHandle.getUserProducts(userID: guestuid)
NotificationCenter.default.addObserver(self, selector: #selector(self.filterRefresh(_:)), name: NSNotification.Name(rawValue: "followRefresh"), object: nil)
} else {
if let user = Auth.auth().currentUser {
if !user.isEmailVerified{
let alert = JDropDownAlert()
alert.alertWith("Please Verify your Email Address")
alert.didTapBlock = {
let myVC = self.storyboard?.instantiateViewController(withIdentifier: "verify") as! VerifyEmailViewController
let navController = UINavigationController(rootViewController: myVC)
self.navigationController?.present(navController, animated: true, completion: nil)
}
} else {
print ("Email verified. Signing in...")
}
}
firebaseControllerHandle.delegate = self
firebaseControllerHandle.userDelegate = self
firebaseControllerHandle.getUserInfo(uid:(Auth.auth().currentUser?.uid)!)
firebaseControllerHandle.getUserProducts(userID: (Auth.auth().currentUser?.uid)!)
}
self.navigationController?.navigationBar.shadowImage = UIImage()
}
@objc func filterRefresh(_ notification:Notification) {
firebaseControllerHandle.delegate = self
firebaseControllerHandle.getUserInfo(uid:guestuid)
firebaseControllerHandle.userDelegate = self
firebaseControllerHandle.getUserProducts(userID: guestuid)
}
@objc func refresh() {
// Code to refresh table view
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "refresh"), object: nil)
// `default` is now a property, not a method call
if guest == true {
firebaseControllerHandle.delegate = self
firebaseControllerHandle.getUserInfo(uid:guestuid)
firebaseControllerHandle.userDelegate = self
firebaseControllerHandle.getUserProducts(userID: guestuid)
} else {
print("ont")
}
refreshControl.endRefreshing()
}
func loadFollowings(followings: [String]) {
let myVC = self.storyboard?.instantiateViewController(withIdentifier: "Following") as! FollowingsViewController
let navController = UINavigationController(rootViewController: myVC)
self.navigationController?.present(navController, animated: true, completion: nil)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
func userInfo(user: UserModel) {
self.user = user
if guest == true {
self.guestUser = user
follower = false
for x in user.followers {
if x == Auth.auth().currentUser?.uid {
follower = true
} else {
follower = false
}
}
print("guest")
self.tableView.reloadData()
} else {
//self.user = user
print("test")
self.tableView.reloadData()
}
}
var follower = false
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "profile", for: indexPath) as! profileCell
if guest == true {
cell.guestUserID = guestuid
if follower == true {
cell.button.setTitle("Following", for: .normal)
cell.buttonChecked = true
} else {
cell.button.setTitle("Follow", for: .normal)
cell.buttonChecked = false
}
cell.bio.text = guestUser.bio
cell.sendMessageButton.setTitle("Send Message", for: .normal)
} else {
let url = URL(string: user.profilePhoto)
print(user.profilePhoto)
cell.img.kf.setImage(with: url)
cell.bio.text = user.bio
cell.button.setTitle("Edit Profile", for: .normal)
cell.sendMessageButton.setTitle("My Orders", for: .normal)
cell.fullname.text = user.fullName
}
cell.delegate = self
return cell
case 1 :
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! profileInfo
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! profileCell
return cell
}
}
```
```
this is my cell button action
@IBAction func followedit(_ sender: Any) {
if buttonChecked == true {
print("REMOVE FOLLOW")
self.delegate?.myTableDelegate()
let appearance = SCLAlertView.SCLAppearance(
showCloseButton: false, showCircularIcon: false
)
// Initialize SCLAlertView using custom Appearance
let alert = SCLAlertView(appearance: appearance)
alert.addButton("Unfollow", backgroundColor: UIColor.black, textColor: UIColor.white) {
let userRef = self.ref.child("Users").child(CurrentUserID.uid).child("Followings")
let query = userRef.queryOrdered(byChild: "userid").queryEqual(toValue: self.guestUserID)
let guestRemove = self.ref.child("Users").child(self.guestUserID).child("Followers")
let guestRemoveQuery = guestRemove.queryOrdered(byChild: "userid").queryEqual(toValue: CurrentUserID.uid)
guestRemoveQuery.observe(.childAdded, with: { snapshot in
if snapshot.exists() {
snapshot.ref.removeValue()
} else {
print("snapshot doesn't exist")
}
})
query.observe(.childAdded, with: { snapshot in
if snapshot.exists() {
snapshot.ref.removeValue()
} else {
print("snapshot doesn't exist")
}
})
}
alert.addButton("Cancel", backgroundColor: UIColor.black, textColor: UIColor.white) {
alert.dismiss(animated: true, completion: nil)
}
alert.showEdit("Unfollow \(guestUserID)", subTitle: "Are you sure you want to unfollow \(guestUserID)?")
} else {
print("ADD FOLLOW")
ref.child("Users").child(CurrentUserID.uid).child("Followings").childByAutoId().setValue(["userid" : guestUserID])
ref.child("Users").child(guestUserID).child("Followers").childByAutoId().setValue(["userid" : CurrentUserID.uid])
print(buttonChecked)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "followRefresh"), object: nil)
}
}
```
[1]: https://i.stack.imgur.com/5sMqa.png
解决方案
推荐阅读
- android - How to use showDialog() to show an untouchable toast message in Flutter?
- python - 为什么将 TextInput 焦点设置为 True,会使 TextInput 不响应键盘输入?
- bash - 使用 git filter-branch 运行 shell 命令
- php - Zend Framework 1:Zend 翻译页面的 404 页面故障排除
- r - 如何在 R 中动态合并行?
- android - ajax jquery 代码在 android -Phonegap- 上不起作用
- sql-server - WSJdbcConnection 不包装任何类型为 com.microsoft.sqlserver.jdbc.SQLServerConnection 的对象
- c# - 无法确定“JContainer”类型的导航属性“JToken.Parent”表示的关系
- ios - 'folly/folly-config.h' 文件未找到
- xaml - Timepicker 中的 24 小时格式