Skip to content

Instantly share code, notes, and snippets.

@zontan
Created July 6, 2020 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zontan/3258f504de5c422d150ad7b3036fd3f2 to your computer and use it in GitHub Desktop.
Save zontan/3258f504de5c422d150ad7b3036fd3f2 to your computer and use it in GitHub Desktop.
import UIKit
import Firebase
class UserSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
var userRef: DatabaseReference!
var friendsRef: DatabaseReference!
var resultsArray = [[String:String]]()
var handle: AuthStateDidChangeListenerHandle?
var currentUser: User?
override func viewDidLoad() {
super.viewDidLoad()
// Set up our database refs
userRef = Database.database().reference(withPath: "users")
friendsRef = Database.database().reference(withPath: "friends")
}
override func viewWillAppear(_ animated: Bool) {
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
self.currentUser = user
// Return to the main screen if we lose our user
if user == nil {
DispatchQueue.main.async {
self.dismiss(animated: true, completion: nil)
}
}
}
}
override func viewWillDisappear(_ animated: Bool) {
if let handle = handle {
Auth.auth().removeStateDidChangeListener(handle)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment