Swift & Firebase Follow / Unfollow ButtonHow to call Objective-C code from Swift#pragma mark in Swift?Swift for loop: for index, element in array?Swift Beta performance: sorting arraysExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callSwift Error - Use of undeclared type 'cell' - Collection ViewSwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift

Is it possible to have the age of the universe be unknown?

How do governments keep track of their issued currency?

Did Milano or Benatar approve or comment on their namesake MCU ships?

Is using haveibeenpwned to validate password strength rational?

Arriving at the same result with the opposite hypotheses

Fail to return int value from a function

Teaching a class likely meant to inflate the GPA of student athletes

Does a scale have more than seven chords?

Why we don’t make use of the t-distribution for constructing a confidence interval for a proportion?

You have (3^2 + 2^3 + 2^2) Guesses Left. Figure out the Last one

Why doesn't Adrian Toomes give up Spider-Man's identity?

What is the purpose of the goat for Azazel, as opposed to conventional offerings?

What ways have you found to get edits from non-LaTeX users?

How to hide an urban landmark?

Overlapping String-Blocks

Has there been a multiethnic Star Trek character?

Which languages would be most useful in Europe at the end of the 19th century?

Meaning of 'lose their grip on the groins of their followers'

Is it legal for a bar bouncer to confiscate a fake ID

How can I make some of my chapters "come to life"?

Jargon request: "Canonical Form" of a word

How to hide rifle during medieval town entrance inspection?

How is John Wick 3 a 15 certificate?

Bent Peugeot Carbolite 103 Frame



Swift & Firebase Follow / Unfollow Button


How to call Objective-C code from Swift#pragma mark in Swift?Swift for loop: for index, element in array?Swift Beta performance: sorting arraysExpand and Collapse tableview cellsUpdate or reload UITableView after completion of delete action on detail viewTableView not displaying text with JSON data from API callSwift Error - Use of undeclared type 'cell' - Collection ViewSwift vertical UICollectionView inside UITableViewHow to search in Firebase Database Swift






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am currently building an application using firebase, i have a follow button which followers the user or unfollows the user if already following.



After i cannot follow the user, when i click the button the data adds into firebase and then gets deleted again..



when i run the app again eveyything works perfectly find apart from the same bug when i am following again
this is my firebase database [FirebaseDatabase][1]



This is my code



 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









share|improve this question

















  • 1





    It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

    – Dhruv Khatri
    Mar 24 at 19:27











  • I fixed it, it was the .childadded firebase query

    – GRattab
    Mar 24 at 21:14

















0















I am currently building an application using firebase, i have a follow button which followers the user or unfollows the user if already following.



After i cannot follow the user, when i click the button the data adds into firebase and then gets deleted again..



when i run the app again eveyything works perfectly find apart from the same bug when i am following again
this is my firebase database [FirebaseDatabase][1]



This is my code



 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









share|improve this question

















  • 1





    It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

    – Dhruv Khatri
    Mar 24 at 19:27











  • I fixed it, it was the .childadded firebase query

    – GRattab
    Mar 24 at 21:14













0












0








0








I am currently building an application using firebase, i have a follow button which followers the user or unfollows the user if already following.



After i cannot follow the user, when i click the button the data adds into firebase and then gets deleted again..



when i run the app again eveyything works perfectly find apart from the same bug when i am following again
this is my firebase database [FirebaseDatabase][1]



This is my code



 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









share|improve this question














I am currently building an application using firebase, i have a follow button which followers the user or unfollows the user if already following.



After i cannot follow the user, when i click the button the data adds into firebase and then gets deleted again..



when i run the app again eveyything works perfectly find apart from the same bug when i am following again
this is my firebase database [FirebaseDatabase][1]



This is my code



 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






swift firebase






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 24 at 18:05









GRattabGRattab

64




64







  • 1





    It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

    – Dhruv Khatri
    Mar 24 at 19:27











  • I fixed it, it was the .childadded firebase query

    – GRattab
    Mar 24 at 21:14












  • 1





    It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

    – Dhruv Khatri
    Mar 24 at 19:27











  • I fixed it, it was the .childadded firebase query

    – GRattab
    Mar 24 at 21:14







1




1





It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

– Dhruv Khatri
Mar 24 at 19:27





It's better you don't add your entire code. Just add Follow and Unfollow Button Events.

– Dhruv Khatri
Mar 24 at 19:27













I fixed it, it was the .childadded firebase query

– GRattab
Mar 24 at 21:14





I fixed it, it was the .childadded firebase query

– GRattab
Mar 24 at 21:14












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326864%2fswift-firebase-follow-unfollow-button%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55326864%2fswift-firebase-follow-unfollow-button%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript