I need some assistance again regarding my codes. Bit confuse on how will the empty data
be reflected from APIService
going to ViewController
.
Here's the JSON
{
"responseMessage": "No record Found",
"data": []
}
As you can see the data
is nil.
Here's the APIService
typealias getDoctorPayoutSummaryTaskCompletion = (_ latestPayoutSummary: DoctorPayoutSummary?, _ error: NetworkError?) -> Void
static func getDoctorPayoutSummary(doctorNumber: String, periodId: Int, completion: @escaping getDoctorPayoutSummaryTaskCompletion) {
guard let latestPayoutSummaryURL = URL(string: "\(Endpoint.LatestCreditedAmount.latestPayoutSummary)?periodId=\(periodId)&doctorNumber=\(doctorNumber)") else {
completion(nil, .invalidURL)
return
}
let sessionManager = Alamofire.SessionManager.default
sessionManager.session.getAllTasks { (tasks) in
tasks.forEach({ $0.cancel() })
}
Alamofire.request(latestPayoutSummaryURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in
guard HelperMethods.reachability(responseResult: response.result) else {
completion(nil, .noNetwork)
return
}
guard let statusCode = response.response?.statusCode else {
completion(nil, .noStatusCode)
return
}
switch(statusCode) {
case 200:
guard let jsonData = response.data else {
completion(nil, .invalidJSON)
return
}
let decoder = JSONDecoder()
do {
let currentPayoutSummary = try decoder.decode(RootDoctorPayoutSummary.self, from: jsonData)
print(periodId)
print(currentPayoutSummary.data ?? "data is nil")
print(currentPayoutSummary.data ?? "response is nil")
completion(currentPayoutSummary.data, nil)
} catch {
completion(nil, .invalidJSON)
print(error)
}
case 400: completion(nil, .badRequest)
case 404: completion(nil, .noRecordFound)
default:
print("**UNCAPTURED STATUS CODE FROM (getDoctorPayoutSummary)\nSTATUS CODE: \(statusCode)")
completion(nil, .uncapturedStatusCode)
}
}
}
I tried to use breakpoints to track my codes and it does print data is nil
if the data
is empty in the APIService
side. But unfortunately the getDoctorPayoutSummary
function in the ViewController
side doesn't recognized if the data is empty. It just recognizes if the data
is not empty and it runs smoothly.
Here's the getDoctorPayoutSummary()
func getDoctorPayoutSummary(doctorNumber: String) {
SVProgressHUD.setBackgroundColor(.lightGray)
SVProgressHUD.show(withStatus: "Processing...")
APIService.DoctorLatestCreditedAmount.getDoctorPayoutSummary(doctorNumber: doctorNumber, periodId: doctorPayoutWeek[0].periodId!) { (payoutsummary, error) in
guard let payoutSummaryDetails = payoutsummary, error == nil else {
if let networkError = error {
switch networkError {
case .noRecordFound:
self.noRecordView.isHidden = false
self.creditedAmountLabel.isHidden = true
case .noNetwork:
let alertController = UIAlertController(title: "No Network", message: "\(networkError.rawValue)", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
default:
let alertController = UIAlertController(title: "Error", message: "There is something went wrong. Please try again", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default))
self.present(alertController, animated: true, completion: nil)
}
}
SVProgressHUD.dismiss()
return
}
self.payoutSummary = payoutSummaryDetails
print(payoutSummaryDetails)
if self.payoutSummary == nil {
self.noRecordView.isHidden = false
SVProgressHUD.dismiss()
return
}
self.creditedAmountLabel.text = "₱\(self.payoutSummary.creditedAmount ?? 0.0)"
self.getPatientList()
self.noRecordView.isHidden = true
self.week1TableView.reloadData()
SVProgressHUD.dismiss()
return
}
}
My apology if I ask too much but I really need help to solve this one so I can move on with other tasks. Asking for your little time to help me.
If data is empty it will return empty array, so check
if currentPayoutSummary.data.isEmpty {
print("Data is empty !!")
completion(nil, .noRecordFound)
}
or
if currentPayoutSummary.data.count == 0 {
print("Data is empty !!")
completion(nil, .noRecordFound)
}