I am a fairly new IOS developer – on the whole everything is going well except…..
I have a program that keeps a list of codes. I can hit a ‘+’ to add a new code and the new view calls a web service to validate the data, and if valid adds it to an internal sqlite database. So far so good.
On entry to the application, a dataview gets updated with all the codes in the database. Again good. If the user selects one of the codes, what is supposed to happen is;
Call another service to get detailed data from a database (SQL server if interested).
Put this data into a initialised table.
Display the data in another view.
The code I am doing is as follows;
In prepareForSegue on first window, I get the code I need and put it into the second view instance.
I clean and prepare the database then exit prepareForeSeque.
All of the above is working!
The viewDidLoad on the second view controller is then called.
It gets the passed in variable, constructs the service request and calls the service.
It then exits and then the another dataview calls the numberOfRowsInSection – which returns zero because the callback from the service which processes the XML data and shoves it in the database has not yet been called!
I did try making a semaphore variable and set it in the service callback once it has processed the data. Then in viewDidLoad I spun waiting for this variable to be set. I spun using [NSSthread sleepForTimeInterval:0.25] but have a suspicion that I am blocking the service callback from being called.
I could ‘cheat’ I believe by putting an intermediate view between my main and sub view – and manually issue the segue to the sub-view – but suspect that this is an overkill!
Stan
You need to call
[self.myTableView reloadData];inside the completion block or delegate method of your web service request.What are you using for the web service calls? I have developed a pretty robust and simple NetworkClient using AFNetworking. All the web service calls have completion blocks where I deal with the data I get back. It will handle dictionaries or arrays from the web service and can do sync or async calls. It also has an optional built in APIKey that can be set and passed along to the web service for additional security. All the web service has to be able to do is handle form POST requests and it works flawlessly.
I do what you are talking about all the time and I just build the array that supports the tableview and then call the
reloadDatamethod on the tableview.