I have Implemented Notificationcenter for Broadcasting data from Delegate page
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector: #selector(self.handleNotification1(withNotification1:)), name: NSNotification.Name(rawValue:"id"), object: nil)
}
I want to call gettenantlistforattendance() function with an API, which have to append array data in tableview array's
@objc func handleNotification1(withNotification1 notification : NSNotification) {
guard let userInfo = notification.userInfo,
let i = userInfo[AnyHashable("fireDrillId")]! as? String else {
print("No userInfo found in notification")
return
}
print("data :: \(userInfo["fireDrillId"])")
let fireID = userInfo["fireDrillId"] as? Int
print("getting id \(fireID ?? 0)")
gettenantlistforattendance()
}
**The code i have implemented in gettenantlistforattendance() for append array and reload tableview is **
func gettenantlistforattendance(){
let headers: HTTPHeaders = [
"Accept": Constants.contentJson,
"Content-Type": Constants.contentJson,
"Authorization": "Bearer " + UserDefaults.standard.getToken()
]
let parameter = ["fireDrillId":self.fireDrillID , "buildingId":userDefaults.getBuildingListData()!.buildingId as Any]
//\(Constants.baseURL)validateOtp
let id = userDefaults.getBuildingListData()!.buildingId
Alamofire.request("\(Constants.baseURL)/tennant/getAttendanceByBuilding?fireDrillId=\(self.fireDrillID)&buildingId=\(String(describing: userDefaults.getBuildingListData()!.buildingId))", method: .get, parameters: parameter, encoding: JSONEncoding.default, headers: headers).responseJSON{
response in
debugPrint(response)
print("from alamofire :: \(response)")
switch response.result
{
case.success(let value):
let swiftyJsonVar = JSON(value)
do {
let totalPresent = swiftyJsonVar["totalPresent"]
let totalAbsent = swiftyJsonVar["totalAbsent"]
let totalMissing = swiftyJsonVar["totalMissing"]
self.totalpresent = "\(totalPresent)"
self.totalabsent = "\(totalAbsent)"
self.totalmissing = "\(totalMissing)"
self.userDefaults.setValue(self.totalpresent, forKey: "present")
self.userDefaults.setValue(self.totalabsent, forKey: "absent")
self.userDefaults.setValue(self.totalmissing, forKey: "missing")
let tenantList = swiftyJsonVar["tenantList"]
if "\(tenantList)" == "null"{
DispatchQueue.main.async {
self.presentCountlbl.text = "00"
self.absentCountlbl.text = "00"
self.missingCountlbl.text = "00"
self.tableviewAttendance.separatorStyle = .none
let alertController = UIAlertController(title: "Sorry", message: "No Data Found !!", preferredStyle: .alert)
let action1 = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction) in
//self.dismiss(animated: true, completion: nil)
print("You've pressed default");
}
alertController.addAction(action1)
self.present(alertController, animated: true, completion: nil)
}
}else{
for arr in tenantList.arrayValue{
self.arrData.append(TernantList_Attendance_Model(json: arr))
}
DispatchQueue.main.async {
self.presentCountlbl.text = self.totalpresent
self.absentCountlbl.text = self.totalabsent
self.missingCountlbl.text = self.totalmissing
self.tableviewAttendance.reloadData() self.tableviewAttendance.separatorStyle = .none
}
}
} catch {
print("error in decoding response from server for login: \(error)")
let alertController = UIAlertController(title: "Invalid Data", message: "Data Not Found !", preferredStyle: .alert)
let action1 = UIAlertAction(title: "OK", style: .default) { (action:UIAlertAction) in
print("You've pressed default");
}
alertController.addAction(action1)
self.present(alertController, animated: true, completion: nil)
}
case.failure(let error):
print("error : \(error.localizedDescription)")
}
}
I have implemented above code for api call.