0
}

Hey guys, I have a problem setting a value for the label. The label should display the number of elements in the array inside my JSON (link - followers_url variable). I call alamo and make a request with that url. When I print the value inside parseData() method I get the right result. When I print it inside configureView() and viewDidLoad() I always get 0.

Setting the label text also works only inside parseData() method. Any ideas how I can get it to work?

2
  • Alamofire call is on another background thread..before the response is received the statement of print after callAlamo is executed. Please do your operation after the completion handler ie response is received in {response in block
    – Mukul More
    Nov 22, 2018 at 8:08
  • Mat I am not sure what you are asking, do you, I mean based on what I understood, if you write followersLabel.text = String(followersAndFollowingArray.count) That should solve the problem. I am assuming that you know these are asynchronous calls and that is why delegate function will need to be used in order to update the code.
    – rptwsthi
    Nov 22, 2018 at 8:09

3 Answers 3

0
Alamofire.request(url).validate().responseJSON { response in
            self.parseData(data: response.data!)
        }

This above request runs on another background thread. So when you call the function callAlamo the response is received in the completion block ( { response in ). So when you call print() after callAlamo. the response has not yet been received and print is called so value is not updated. So please perform the operation on the response only through completion block. If you want to set a label write you set label code after self.parseData in completion block ({response in). Make sure you set it in main queue as the UI operation needs to be performed on main queue only

Following question will help to set label on main thread.

In Swift how to call method with parameters on GCD main thread?

You need to understand multithreading concept to get a better understanding of this. Follow this https://medium.com/@gabriel_lewis/threading-in-swift-simply-explained-5c8dd680b9b2

0

You should learn something about iOS Parsing techniques. Then learn how to create Model using class or struct. Then you will get Idea.

You should look into Object Mapper as well.

0

You're dealing with an asynchronous operation. Asynchronous operations are "actions" that are dispatched and require you to wait before they complete. Think about loading a website in Safari. Once you type, let's say, stackoverflow.com in your browser, a loading spinner will notify that something is loading. While the page is loading, you obviously cannot see what's on the webpage. There's only an empty, white page.

The same is happening with your request. When you call the callAlamo function you're telling the app to start loading something. This is requiring you to wait until the task is done. If you count the elements in the followersAndFollowingArray right after the server call, then you'll get it empty, because the request is still waiting to be completed. It's like pretending to view the stackoverflow.com website immediately after having typed the URL. You can't.

That's where closures come in handy. You can use closures to execute something when another action has been completed. In this case, I would fire the web request, display a loading spinner to notify the user that something is loading, and finally populate the followersLabel along with stopping the animation. You can do something like that

func callAlamo(url: String, completion: @escaping ([User]) -> Void) {
    if Connectivity.isConnectedToInternet {
        Alamofire.request(url).validate().responseJSON { response in
            let userData = self.parseData(data: response.data!)
            completion(userData)
        }
    }
}

Additionally you need to let the parseData method to return the parsed array of Users, so the callAlamo function could use it.

func parseData(data : Data) -> [User] {
    do {
        return try JSONDecoder().decode([User].self, from: data)
    } catch let jsonErr {
        print("Error serializing", jsonErr)
        return [User]()
    }
}

Finally, you can execute the callAlamo function on inside the configureView method, performing an action when the server request has been completed. In our case, we want to populate the label.

private func configureView(){
    followersLabel.text = String(followers)

    // Starting the loading animation
    startAnimation()

    callAlamo(url: "Hello") { userData in
        // Assigning the callAlamo result to your followers array
        // once the server request has been completed
        self.followersAndFollowingArray = userData

        // This will return the number you'd expect
        print(self.followersAndFollowingArray.count)

        // Stopping the loading animation
        stopAnimation()
    }
}

Right now you probably won't have the startAnimation and stopAnimation methods, but you can feel free to implement them, I just wanted to give you an idea of a classic implementation.

1
  • @MattAndrei ops, you're right, I forgot that you're dealing with a do/catch block. I fixed the parseData method to work correctly and avoid that error :) Nov 22, 2018 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.