0

I am new to iOS objective C and currently am enhancing some features to an existing project. I would like to implement a callback method to return the results from an API call in the viewDidLoad method as I need the results to determine the number of icons to display on the screen.

I have looked around for various answers, however, I am not sure what has went wrong and I am unable to return the responses from the APIs to the NSMutableArray in the ViewDidLoad method. I would greatly appreciate any help on how to go about get the response into the customerArray in the viewDidLoad method

This is the Service Delegate header file that defines the method: enter image description here

I made use of the first method requestCompleted in my ViewController file to retrieve the response returned from the API. Within this method, customerArray which is a globally declared array contains the items that was returned from the API, however when this array was called in the viewDidLoad method, it appears to be null. enter image description here

I tried the following way to retrieve using the dispatch async method, but the customer Array is still empty

enter image description here

Richard Rodjues
  • 207
  • 1
  • 6
  • 23
  • - Where do you define customerArray? - If you defined customerArray as a property, you will need to use self.customerArray = dict – Nguyen Loc Nov 17 '20 at 09:46
  • Hey that looks like my exact dispatch example from [this question!](https://stackoverflow.com/questions/16283652/understanding-dispatch-async/16283719#16283719) That's cool! Anyways, I'm not understanding where you're making your API call. I assume your ServiceAgent class is a custom one, right? Where in the pattern does it call the delegate methods? It looks like you're calling `requestCompleted` manually, so I'm guessing you didn't implement the pattern correctly. Also, jsyk, NSJSONSerialization does not return a mutable array. You have to add `mutableCopy` to make it mutable. – Liftoff Nov 17 '20 at 22:18
  • The idea behind the dispatch is to put your API call in the background thread part and then put your UI changes in the Run UI Updates part. You can handle this in many different ways as well. NSURLRequests have their own lifecycle with completion methods where you can make a callback. – Liftoff Nov 17 '20 at 22:23

2 Answers2

0

You are printing customerArray, before dispatch_async executed. Try putting breakpoint in requestCompleted method or put print statement in requestCompleted method. and check the value.

Manish Punia
  • 747
  • 4
  • 9
-2

You can't execute the callback method in viewDidLoad function. And then this attempt is not good pattern in iOS.
I understand what you want. You want to display various icons when view controller is appeared.
For this, you may need to use UITableView or CollectionView. In this case, i have been doing following.
First, Run API call by using Alamofire
Second. Show loading icon (by using HUD)
Third, In API callback, fetch the list information and reload tableview.
*Following is swift code but it is very similar with Objective C.

override func viewDidLoad() {
    super.viewDidLoad()

    HUD.show(.rotatingImage(UIImage(named: "rotateLogo")), onView: nil)
    
    APIManager.request(Constant.sdkCredential,
                       parameters: [:],
                       fullResponse:true)
    { (response, success, error) in
        if success == true {
            guard (response as? Dictionary<String,Any>) != nil else {
                self.showAlert(withTitle: "Error", message: "Invalid Credential")
                return
            }
            
            if let dict = response as? Dictionary<String,Any> {
                
                print(dict)
                
                if let dictData = dict["data"] as? Dictionary<String,Any>{
                    self.tableView.reloadData();
                }
            }
        } else {
            HUD.hide()
            self.showAlertWithError(error)
        }
    }
}
Harry Jin
  • 54
  • 7