PDA

View Full Version : Wait until data is loaded from a server with json




Mac2011
Jan 28, 2011, 11:20 AM
So my question is: How can I wait to load the tableview until the data is loaded.
Unfortunately I can't use connectionDidFinishLoading in a while loop because it's a void message.
Any suggestions?



ChrisA
Jan 28, 2011, 11:47 AM
So my question is: How can I wait to load the tableview until the data is loaded.
Unfortunately I can't use connectionDidFinishLoading in a while loop because it's a void message.
Any suggestions?

On other platforms I've used, a writer can lock the table. It will not write until it gets the lock. The reader if it issues a read will block until the lock is removed by the writer. On many systems this is effisent as no polling is involved the system only checks when lock states change

Mac2011
Jan 28, 2011, 12:05 PM
Thx. I'll try it. Maybe there is a way that I lock the "main" thread until a second thread is done or a queue of things it should do.

cnstoll
Jan 28, 2011, 01:08 PM
Sounds like you're going about it the wrong way. You should let the tableView go ahead and load empty when the view is loaded. Once the data has been downloaded and stored in your datasource you should set up a delegate method in your table view controller to update the table view to display your new data.

A great way to update the table view is:

[tableView beginUpdates];

// insert some rows, do whatever updates

[tableView endUpdates];

That will animate all of the updates in the begin/end block in a very nice way. It's pretty neat. For more information check out the documentation on UITableView.

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html

Mac2011
Jan 29, 2011, 06:21 PM
Wirelessly posted (Mozilla/5.0 (iPod; U; CPU iPhone OS 4_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8F5153d Safari/6533.18.5)

Yep. Now I got. Very simple. I created a NSoperationqueue in viewdidload and a delegate. after the connection did finish launching method, a method gets called to set the data for the table view and than reload its data. I think that's the best way to go and the UI doesn't get stuck either.

PhoneyDeveloper
Jan 29, 2011, 08:24 PM
If you like you can add a row to the table that says Loading... or something like that and then remove it when the data arrive.

LazyTableImages does something like this.

xingworld
Jan 30, 2011, 05:09 AM
There is a sample named LazyTableImages in xcode help document.
I think it's just what you wanted.
:)