1

Following up with the post: Can AFNetworking return data synchronously (inside a block)?

One of the comments in that post was:

The trick to asynchronous programming is to break the procedural, synchronous assumption that data is there when you ask for it. Instead, with async, when you ask for something, you give it a callback to perform when the data finally is ready. In this case, you would call the block in the success block of the JSON operation. Rather than the method returning data, it's told what to do when the data is finished downloaded.

Although I'm using GCD and asynchronous downloading on iOS, I do not really understand how to implement this "procedural break" when programming with async.

For example, assume I need to download some JSON data, which includes lots of data including an image URL. I will have to download the actual image afterwards.

Each cell in the table takes in the data from the JSON/images downloaded. How would I implement this procedural break in this case?

Community
  • 1
  • 1
darksky
  • 20,411
  • 61
  • 165
  • 254

1 Answers1

1

While your data has not arrived, your table view dataSource tells its table view that it has zero rows, and displays a spinner. When the callback is fired, you store the data somewhere, remove the spinner, and call [tableView reloadData]. Basically, that's all there is to it.

Cyrille
  • 25,014
  • 12
  • 67
  • 90
  • Agreed. While `reloadData` sometimes is appropriate (e.g. you loaded all of the data for the table via JSON), but frequently when working with table views, you just update the indexPath in question, or perhaps even a particular control on that `UITableViewCell`. – Rob Jan 26 '13 at 17:01