Home > Enterprise >  Upload Empty Strings to Firebase Realtime Database
Upload Empty Strings to Firebase Realtime Database

Time:08-29

I'm trying to upload a child nod to a reference to my Firebase Realtime Database, however I'm running into an issue. I'm trying to have my secondDateAndTimes and thirdDateAndTimes upload as empty strings if they are not filled out, but my handleConfirm() will only work if the two are filled out. What do I need to do so the child node uploads even if the two UILabels are empty/not showing? Thank you!

JobRequest Model

struct JobRequest {
    
    let uid: String
    let fullname: String
    let username: String
    let address: String
    let firstDateAndTimes: String
    let secondDateAndTimes: String
    let thirdDateAndTimes: String
    let timeConstraints: String
    let jobDescription: String
    
    init(dictionary: [String : Any]) {
        self.uid = dictionary["uid"] as? String ?? ""
        self.fullname = dictionary["fullname"] as? String ?? ""
        self.username = dictionary["username"] as? String ?? ""
        self.address = dictionary["address"] as? String ?? ""
        self.firstDateAndTimes = dictionary["firstDateAndTimes"] as? String ?? ""
        self.secondDateAndTimes = dictionary["secondDateAndTimes"] as? String ?? ""
        self.thirdDateAndTimes = dictionary["thirdDateAndTimes"] as? String ?? ""
        self.timeConstraints = dictionary["timeConstraints"] as? String ?? ""
        self.jobDescription = dictionary["jobDescription"] as? String ?? ""
    }
    
}

API Service

struct JobDetailCredentials {
    var uid: String
    var fullname: String
    var username: String
    var address: String
    var firstDateAndTimes: String
    var secondDateAndTimes: String
    var thirdDateAndTimes: String
    var timeConstraints: String
    var jobDescription: String
}

// MARK: - CustomerService

struct CustomerService {

static func uploadCustomerAndJobRequest(jobDetails: JobDetailCredentials, completion: @escaping(DatabaseCompletion)) {
        
        guard let uid = Auth.auth().currentUser?.uid else { return }
        
        let values = ["uid" : jobDetails.uid,
                      "fullname" : jobDetails.fullname,
                      "username" : jobDetails.username,
                      "address" : jobDetails.address,
                      "firstDateAndTimes" : jobDetails.firstDateAndTimes,
                      "secondDateAndTimes" : jobDetails.secondDateAndTimes,
                      "thirdDateAndTimes" : jobDetails.thirdDateAndTimes,
                      "timeConstraints" : jobDetails.timeConstraints,
                      "jobDescription" : jobDetails.jobDescription] as [String : Any]
        
        
        REF_CUSTOMERS.child(uid).child("job-request-details").childByAutoId().updateChildValues(values, withCompletionBlock: completion)
}

}

RequestJobConfirmationController

lazy var secondDateAndTimeDetailsLabel: UILabel = {
    let label = UILabel()
    label.textColor = .black
    label.textAlignment = .center
    label.font = UIFont(name: "AvenirNext-MediumItalic", size: 14)
    label.numberOfLines = 0
    return label
}()
    
lazy var thirdDateAndTimeDetailsLabel: UILabel = {
    let label = UILabel()
    label.textColor = .black
    label.textAlignment = .center
    label.font = UIFont(name: "AvenirNext-MediumItalic", size: 14)
    label.numberOfLines = 0
    return label
}()

@objc func handleConfirm() {
        
        guard let uid = Auth.auth().currentUser?.uid ,
              let fullname = fullnameDetailsLabel.text ,
              let username = customer?.username ,
              let address = addressDetailsLabel.text ,
              let firstDateAndTimes = firstDateAndTimeDetailsLabel.text ,
              let secondDateAndTimes = secondDateAndTimeDetailsLabel.text ,
              let thirdDateAndTimes = thirdDateAndTimeDetailsLabel.text ,
              let timeConstraints = timeConstraintsDetailsLabel.text ,
              let jobDescription = jobDescriptionDetailsLabel.text else { return }
        
        let jobDetails = JobDetailCredentials(uid: uid, fullname: fullname, username: username, address: address,
                                              firstDateAndTimes: firstDateAndTimes, secondDateAndTimes: secondDateAndTimes,
                                              thirdDateAndTimes: thirdDateAndTimes, timeConstraints: timeConstraints,
                                              jobDescription: jobDescription)
        
        CustomerService.uploadCustomerAndJobRequest(jobDetails: jobDetails) { error, ref in
            if let _ = error {
                self.simpleAlert(title: "Error", msg: "Unable to upload job details, please try again later.")
                return
            }
            
            let controller = FindEmployeeJobRequestController()
            controller.modalPresentationStyle = .fullScreen
            self.present(controller, animated: true, completion: nil)
        }
        
}

CodePudding user response:

Instead of putting your secondDateAndTimes and thirdDateAndTimes inside the guard, which will guarantee that the function returns if they're nil, move them outside of that guard and optionally bind them:

guard let uid = Auth.auth().currentUser?.uid ,
              let fullname = fullnameDetailsLabel.text ,
              let username = customer?.username ,
              let address = addressDetailsLabel.text ,
              let firstDateAndTimes = firstDateAndTimeDetailsLabel.text ,
              let timeConstraints = timeConstraintsDetailsLabel.text ,
              let jobDescription = jobDescriptionDetailsLabel.text else { return }

let secondDateAndTimes = secondDateAndTimeDetailsLabel.text ?? "",
let thirdDateAndTimes = thirdDateAndTimeDetailsLabel.text ?? "",

//...

CodePudding user response:

I figured what my problem was, I wasn't setting empty strings as the texts for my UILabels.

RequestJobConfirmationController

lazy var secondDateAndTimeDetailsLabel: UILabel = {
    let label = UILabel()
    label.text = ""
    label.textColor = .black
    label.textAlignment = .center
    label.font = UIFont(name: "AvenirNext-MediumItalic", size: 14)
    label.numberOfLines = 0
    return label
}()
    
lazy var thirdDateAndTimeDetailsLabel: UILabel = {
    let label = UILabel()
    label.text = ""
    label.textColor = .black
    label.textAlignment = .center
    label.font = UIFont(name: "AvenirNext-MediumItalic", size: 14)
    label.numberOfLines = 0
    return label
}()
  • Related