I’m working on app, which sends and receives JSON data using NSURLConnection, but I have this connection and its delagate methods inside viewcontroller, and that’s not correct for MVC I guess. I’ve wanted separated class for whole connection, but there was problem that I couldn’t detect then in viewcontroller, when the connection finished loading, so I couldn’t get received data. What is the correct way to do this ?
However, this hole (non MVC) NSURLConnection inside viewcontrollers works fine for my app, except one case – when I need to use tableview, which is part of viewcontroller – which is also datasource and delegate for this tableview. It seems like tableview is trying to create cells, before connection finished loading, which is little bit weird I think, because in method tableView:numberOfRowsInSection: I already have data I need, and I thought that this method is called before I’m configuring cell in tableView:CellForRowAtIndexPath: …but I’m wrong probably. Can somebody explain me what’s going on here and how to solve this ? (also the first part with correct using of MVC with NSURLConnection)
Implement a delegate protocol for your JSON client class, e.g.:
Then, have your viewcontroller implement the delegate protocol:
Note that this is an extremely abbreviated example – I’ve cut a few corners to save some space. I trust you can read between the lines and fill in the gaps – if not (i.e. my example is unclear), let me know and I’ll elaborate further.