save data code
unknown
swift
4 years ago
1.7 kB
27
Indexable
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.deque(class: UserCell.self, for: indexPath)
let uid = Auth.auth().currentUser?.uid
let ref = Database.database(url: FirebaseHelper.dbURL).reference()
let key = ref.child("users").childByAutoId().key
let selectedItem = users[indexPath.row]
var isFollowing = false
ref.child("users").child(uid!).child("following").queryOrderedByKey().observeSingleEvent(of: .value) { snapshot in
if let following = snapshot.value as? [String : AnyObject] {
for (ke, value) in following {
if value as! String == selectedItem.username {
isFollowing = true
ref.child("users").child(uid!).child("following/\(ke)").removeValue()
ref.child("users").child(selectedItem.userID!).child("followers/\(ke)").removeValue()
self.tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
}
}
if !isFollowing {
let following = ["following/\(key)" : selectedItem.username]
let followers = ["followers/\(key)" : selectedItem.username]
ref.child("users").child(uid!).updateChildValues(following)
ref.child("users").child(selectedItem.userID!).updateChildValues(followers)
self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
}
ref.removeAllObservers()
}Editor is loading...