• Refactoring performRegistration
  • Save Image to Firebase
  • Save Infos to Firestore

  • Reian cycle, weak, unowned
    fileprivate func saveImageToFirebase(completion: @escaping (Error?) -> ()) {
        let filename = UUID().uuidString
        let ref = Storage.storage().reference(withPath: "/images/\(filename)")
        let imageData = self.bindableImage.value?.jpegData(compressionQuality: 0.75) ?? Data()
        ...
    }
    func performRegistration(completion: @escaping (Error?) -> ()) {
        guard let email = email, let password = password else { return }
        self.bindableIsRegisterating.value = true
        Auth.auth().createUser(withEmail: email, password: password) { (res, err) in
            
            if let err = err {
                completion(err)
                return
            }
            
            print("Successfully registered user : ", res?.user.uid ?? "")
            self.saveImageToFirebase(completion: completion)
        }
    }

We can see what’s going on clearly

    fileprivate func saveInfoToFirestore(completion: @escaping (Error?) -> ()) {
        let uid = Auth.auth().currentUser?.uid ?? ""
        let docData = ["fullName": fullName ?? "", "uid": uid]
        Firestore.firestore().collection("users").document(uid).setData(docData) { (err) in
            if let err = err {
                completion(err)
                return
            }
            
            completion(nil)
            // which means everything is able to finish correctly
        }
    }

To save infos including images in firebase(which is stored as url) add a new argument of func saveInfoToFirestore

fileprivate func saveInfoToFirestore(imageUrl: String, completion: @escaping (Error?) -> ())
        self.saveInfoToFirestore(imageUrl: imageUrl, completion: completion)

docData는 dictionary가 적합한 듯하다. 여러 데이터를 저장해야하기 때문!

// func saveInfoToFirestore
    let docData = ["fullName": fullName ?? "", "uid": uid, "imageUrl1" : imageUrl]

Let’s check out the firestore code one more time

Firestore.firestore().collection("users").document(uid).setData(docData)

Retain cycle, weak, unowned

블로그 번역

Brian’s video clip

태그: ,

카테고리:

업데이트:

댓글남기기