• Fetch current user in HomeController
  • new use variable in HomeController
  • Delegation for saving settings and refetching data
  • protocol
  • fetchCurrentUser()

query - where

        let query = Firestore.firestore().collection("users").whereField("age", isGreaterThanOrEqualTo: 20).whereField("age", isLessThanOrEqualTo: 44)

make some code as comments

    override func viewDidLoad() {
        super.viewDidLoad()
        
        topStackView.settingsButton.addTarget(self, action: #selector(handleSettings), for: .touchUpInside)
        bottomControls.refreshButton.addTarget(self, action: #selector(handleRefresh), for: .touchUpInside)
        
        setupLayout()
//        setupFirestoreUserCards()
//        fetchUsersFromFirestore()
    }

fetch current user

Actually we implemented it in SettingsController before

        guard let uid = Auth.auth().currentUser?.uid else { return }
        Firestore.firestore().collection("users").document(uid).getDocument { (snapshot, err) in
            if let err = err {
                print(err)
                return
            }
            
            // fetch our user here
            guard let dictionary = snapshot?.data() else { return }
            self.user = User(dictionary: dictionary)
            self.loadUserPhotos()
            self.tableView.reloadData()
        }

new user variable for HomeController

    fileprivate var user: User?

    fileprivate func fetchCurrentUser() {
      ...
            print("Current user:", self.user)

Now we can get min, max Seeking Age of User

        guard let minAge = user?.minSeekingAge, let maxAge = user?.maxSeekingAge else { return }
        
        let query = Firestore.firestore().collection("users").whereField("age", isGreaterThanOrEqualTo: minAge).whereField("age", isLessThanOrEqualTo: maxAge)

Delegation for saving settings and refetching data

handleSave

    @objc fileprivate func handleSave() {
        ...
        Firestore.firestore().collection("users").document(uid).setData(docData) { (err) in
            ...
            print("Finished saving user info")
            self.dismiss(animated: true, completion: {
                print("Dismissal complete")
                // homeController.fetchCurrentUser()
                // I want to refetch my cards inside of homeController somehow
            })
        }
    }

What’s the link?! -> Delegation

Let’s make a protocol

we don’t need the definition of this function

protocol SettingsControllerDelegate {
    func didSaveSettings()
}
class SettingsController: UITableViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var delegate: SettingsControllerDelegate?
// handleSave()
            self.dismiss(animated: true, completion: {
                print("Dismissal complete")
                // homeController.fetchCurrentUser()
                // I want to refetch my cards inside of homeController somehow
                self.delegate?.didSaveSettings()
            })

이 상태에서 실행하면 delegate는 null이다.

아직 위임을 하지 않았기 때문이다.

delegate 지정은 homeController에서 settingsController를 생성할 때 해준다.

    @objc func handleSettings() {
        let settingsController = SettingsController()
        settingsController.delegate = self

We need to confirm that we defined the protocol to utilize settingsController.delegate

class HomeController: UIViewController, SettingsControllerDelegate {
    func didSaveSettings() {
        print("Notified of dismissal from SettingsController in HomeController")
        self.fetchCurrentUser()
    }

fetchUsersFromFirestore() is in fetchCurrentUser()

Controller간 서로의 메소드를 사용하고싶을 때에는 Delegate를 사용할 수 있다.

Refactoring

add JGProgressHUD

    fileprivate let hud = JGProgressHUD(style: .dark)
    fileprivate var user: User?
    
    fileprivate func fetchCurrentUser() {
        hud.textLabel.text = "Loading"
        hud.show(in: view)

Remove all subviews!

        cardsDeckView.subviews.forEach({$0.removeFromSuperview()})

SettingsController.swift

    fileprivate func fetchCurrentUser() {
        Firestore.firestore().fetchCurrentUser { (user, err) in
            if let err = err {
                print("Failed to fetch user: ", err)
                return
            }
            self.user = user
            self.loadUserPhotos()
            self.tableView.reloadData()
        }
    }

태그: ,

카테고리:

업데이트:

댓글남기기