viewDidLoad not getting called for item on UITabViewController - ios

on a button click ,via segue the flow goes to UITabViewController.
self.performSegueWithIdentifier(self.gotoResult, sender: nil)
let myUrl = NSURL(string: "XXXXXXXX");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
resultVar.city = cityText.text
resultVar.state = streetText.text
let postString = "streetaddr=\(streetText.text)&city=\(cityText.text)&state=\(stateVal)&degree=\(degreeVal)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
print("error= \(error)")
return
}
// You can print out response object
print("response = \(response)")
// Print out response body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
do {
resultVar.myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
} catch let error2 as NSError? {
print("error 2 \(error2)")
}
}
task.resume()
There is a UITabViewController with three items. when the initial view gets loaded(item1) the viewDidLoad is not getting called. for now I have added the same code in viewDidAppear and when i click on a different tab and come back to item1 ,the fields are populated. But I want it to work on initial load after the segue only. What am I missing?
ViewdidLoad of Item1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("in here CityViewController")
if let parseJSON = resultVar.myJSON {
// Now we can access value of elements by its key
var weather_condition = parseJSON["weather_condition"] as! String
print("weather_condition: \(weather_condition)")
weatherconditionLbl.text = weather_condition
}
The code which gets called when i switch tabs:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("in here CityViewController1")
if let parseJSON = resultVar.myJSON {
// Now we can access value of elements by its key
var weather_condition = parseJSON["weather_condition"] as! String
print("weather_condition: \(weather_condition)")
weatherconditionLbl.text = weather_condition + "in " + resultVar.city+","+resultVar.state
}
}
so the code viewdidload is not getting called and viewDidAppear gets called when i switch tabs.

A network request will be slower than rendering the next scene so "parseJSON" is not there. You need to refresh your graphics using a callback from the network request. In order to do that I suggest that you call the network request from "Item 1" every time you need to refresh its content (that is up to you).

Related

Avoid re-loading UITableView data

I have a problem where a table shows a long list of drills that I want to remain at their current scroll position when user navigates away and then returns.
In my view controller, I have code that loads the data for a UITableView whenever the view appears by calling getDrillList(optimize: true) which stores the data in a property called drillListArray.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
getDrillList(optimize: true)
}
Here is the code that loads the data
private func getDrillList(optimize: Bool = false)
{
// MAKE API CALL, THE ARRAY IS POPULATED IN THE COMPLETE HANDLER
let appDelegate = UIApplication.shared.delegate as! AppDelegate
SharedNetworkConnection.apiGetDrillList(apiToken: appDelegate.apiToken, limit: (optimize ? 13 : 0), completionHandler: { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
// 403 on no token
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
SharedNetworkConnection.apiLoginWithStoredCredentials(completionHandler: { data, response, error in
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let json = try? JSONSerialization.jsonObject(with: data!, options: [])
if let dictionary = json as? [String: Any] {
if let apiToken = dictionary["token"] as? (String) {
appDelegate.apiToken = apiToken
self.getDrillList()
}
}
})
return
}
self.drillListParser = DrillListParser(jsonString: String(data: data, encoding: .utf8)!)
self.drillListArray = (self.drillListParser?.getDrillListArray())!
DispatchQueue.main.async {
self.drillTableView.reloadData()
}
if optimize {
self.getDrillList()
}
})
}
Two questions
First, if user is shown a segue to another view controller and then returns via Back, how can I check if the data is already loaded to avoid loading it a second time? Is it safe to check if the array is empty?
Second, are there any reprocussions I should be aware of with this approach?
Add getDrillList(optimize: true) in viewDidLoad() and it will calls once in lifecycle or you can put a check that checks if already loaded a data or not via a boolean flag.

Swift - View loads before http request is finished in viewDidLoad()

I am trying to load a values from a database and put them into a UITableView in the viewDidLoad function in one of my Swift files. When debugging, at the time of the view rendering, the list of values is empty, but after the view loads, the list gets populated by the view loads. I don't have much experience with threads in Swift, so I am not exactly sure why this is happening, any ideas? I have tried to run DispatchQueue.main.async, but that did not work My code is below:
override func viewDidLoad() {
super.viewDidLoad()
// Load any saved meals, otherwise load sample data.
loadDbMeals()
}
private func loadDbMeals() {
var dbMeals = [Meal]()
let requestURL = NSURL(string: self.URL_GET)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "GET"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//parsing the response
do {
//converting response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: [.mutableContainers]) as? NSDictionary
//parsing the json
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
}
}
} catch {
print(error)
}
}
meals += dbMeals
//executing the task
task.resume()
}
So, the current order of breakpoints, is the call to loadDbMeals() in the viewDidLoad() function, then it tries to add the dbMeals variables to the global meals variable, and then the http request gets executed, after the empty list has already been added. I appreciate any help!
Reload your table after loading data
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
the request happens asynchronously. so the view is loaded while the request may still be in progress.
move the meals += dbMeals line into the request's completion handler (after the for loop), add a self. to the meals var since you are referencing it from within a closure and reload the tableview from the main thread afterwards:
DispatchQueue.main.async{
self.tableView.reloadData()
}
Because dataTask is not a synchronised call, we need to use lock to wait until all fetch is finished.
Code will look something like this:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async {
let lock = DispatchSemaphore(value: 0)
// Load any saved meals, otherwise load sample data.
self.loadDbMeals(completion: {
lock.signal()
})
lock.wait()
// finished fetching data
}
}
private func loadDbMeals(completion: (() -> Void)?) {
var dbMeals = [Meal]()
let requestURL = NSURL(string: self.URL_GET)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "GET"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//parsing the response
do {
//converting response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: [.mutableContainers]) as? NSDictionary
//parsing the json
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
}
}
} catch {
print(error)
}
// call completion
completion()
}
meals += dbMeals
//executing the task
task.resume()
}
So execute loadDbMeals with completion block which will be called when fetching is finished and lock will wait until completion block is called.

Table only shows when interacting and not by default

I have a table in a view controller that is populated through a dictionary from which information is retrieved via a JSON request. In the viewDidLoad() function, I call the function that retrieves the data which is added to `IncompletedDeadlines dictionary:
override func viewDidLoad() {
super.viewDidLoad()
self.IncompleteDeadlines = [String:AnyObject]()
self.retrieveIncompletedDeadlines()
}
Everything works however the table only shows when interacted with. I thought maybe the best way to show the table the moment the view appears is by adding a tableView.reload to viewDidAppear as so:
override func viewDidAppear(_ animated: Bool) {
self.tableView.reloadData()
}
But this doesn't fix it. I have attached pictures for clarity of the situation. Picture one shows the view the moment the view appears. Picture 2 only happens once the table is interacted with i.e. swiped. So my question is how can I get the table to show immediately? I understand there can be a delay because of the load, but I shouldn't have to interact with it for it to show:
When the view is interacted with i.e. swiped:
The retrieveIncompletedDeadlines() function is as so:
func retrieveIncompletedDeadlines(){
let myUrl = NSURL(string: "https://www.example.com/scripts/retrieveIncompleteDeadlines.php");
let request = NSMutableURLRequest(url:myUrl! as URL)
let user_id = UserDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let checker:String = parseJSON["status"] as! String;
if(checker == "Success"){
let resultValue = parseJSON["deadlines"] as! [String:AnyObject]
self.IncompleteDeadlines = resultValue
}
self.tableView.reloadData()
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
self.tableView.reloadData()
}
JSON will be parsed on the background thread but any update to the UI must be done on the main thread hence you have to do it inside DispatchQueue.main.async {} This article explains well what is the problem.
Furthermore I would write a completions handler which returns the data once the operation has finished. This is another interesting article about.
Completion handlers are super convenient when your app is doing something that might take a little while, like making an API call, and you need to do something when that task is done, like updating the UI to show the data.
var incompleteDeadlines = [String:AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
//please note your original function has changed
self.retrieveIncompletedDeadlines { (result, success) in
if success {
// once all the data has been parsed you assigned the result to self.incompleteDeadlines
self.incompleteDeadlines = result
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
func retrieveIncompletedDeadlines(_ completion:#escaping ([String:AnyObject] , _ success: Bool)-> Void){
let myUrl = NSURL(string: "https://www.example.com/scripts/retrieveIncompleteDeadlines.php");
let request = NSMutableURLRequest(url:myUrl! as URL)
let user_id = UserDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let checker:String = parseJSON["status"] as! String;
var resultValue = [String:AnyObject]()
if(checker == "Success"){
resultValue = parseJSON["deadlines"] as! [String:AnyObject]
}
completion(resultValue, true)
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
}
}

PushViewController doesn't work after httpPost

I have made a login screen which takes the input and communicates with the REST api to verify the user. If the response is true, I login the user else not.
I have written a method openViewControllerBasedOnIdentifier(id) to switch views.
The REST api returns true and false appropriately. The push controller gets called but view does not change. How if I place only one line in LoginAction method 'self.openViewControllerBasedOnIdentifier("PlayVC")' and remove the rest of the code , it works fine.
Here is my code
#IBAction func LoginAction(_ sender: Any) {
//self.openViewControllerBasedOnIdentifier("PlayVC")
Constants.login_status = false
//created NSURL
let requestURL = NSURL(string: URL_BK)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "POST"
let username = phonenumber.text
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "username="+username!+"&password=bk&schoolId=0";
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
let responseData = String(data: data!, encoding: String.Encoding.utf8)
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
print(“Received data is ---%#",responseData as Any)
let myJSON = try JSONSerialization.jsonObject(with: data! , options: .allowFragments) as? NSDictionary
if let parseJSON = myJSON {
var status : Bool!
status = parseJSON["status"] as! Bool?
//print(status)
if status==false
{
Constants.login_status = false
}
else{
Constants.login_status = true
print("calling PLAYVC")
self.openViewControllerBasedOnIdentifier("PlayVC")
}
}
else{
print("NULL VALUE RECEIVED")
}
} catch {
print(error)
}
}
//executing the task
task.resume()
}
You should open the new view controller on the main thread like this:
DispatchQueue.main.async {
self.openViewControllerBasedOnIdentifier("PlayVC")
}
Your REST API query response is processed in a background thread when you call URLSession.shared.dataTask and so when you call any UI actions, you should wrap the code as above to execute the UI code in the main thread. Then it would work fine :)

I can't send data to the next UIViewController

I need to send this data(id and status) to the next UIViewController
{"success":true,"id":37,"status":"WAITING"}
but happens this in output....
id: nil // Nextview
status try: nil // nextView
ID NEXT VIEW 37 // the present view
status NEXT VIEW WAITING // the present view
id: Optional(37) // the next view
status try: Optional("WAITING") // the next view
This is my method to get a json and parse, and after, with method, send to the next view controller.
func sendOrder(jsonOrder: AnyObject){
let request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.0.21:8080/easy-coffee/get-order/")!)
request.HTTPMethod = "POST"
let postString = jsonOrder
print(postString)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(responseString)
do{
let OrderDict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
self.orderId = OrderDict["id"] as? Int
self.orderStatus = OrderDict["status"] as? String
self.nextView(self.orderId, orderStatus: self.orderStatus)
}catch{
}
}
task.resume()
}
Method to send data to the next UIView Controller
func nextView(orderId: Int, orderStatus: String){
if let waitingController = storyboard!.instantiateViewControllerWithIdentifier("waitingOrder") as? WaitingOrderViewController {
print("ID NEXT VIEW \(orderId)")
print("status NEXT VIEW \(orderStatus)")
waitingController.id = orderId
waitingController.status = orderStatus
self.presentViewController(waitingController, animated: true, completion: nil)
}
Thank you guys
send the data as NSDictionary, in the next view controller create a dictionary bar and in the segue properties set this.

Resources