iPhone JSON asynchronous coding [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post开发者_StackOverflow社区.
Closed 5 years ago.
Improve this questionI have a place in my program where I need to send some data to the server, get a response, and put it into a dictionary.
I'm quite new to this, and until now, I have been sending a synchronous request to the server. This is easy, because I can just get the response and set it in my dictionary through the function that does the call.
However, doing things asynchronously clearly complicates matters.
How can I easily and elegantly determine when my request has finished processing, and then continue doing what I was doing, without locking up the entire program?
Thankful for any and all answers / tutorials.
Edit: As a response to the comment below, what I find confusing is that my response will show up in connectionDidFinishLoading after an indeterminate amount of time, and I really don't know how to continue doing what I was doing in the method that called the request, without taking a wild guess with NSTimer or whatever.
NSNotification would be a great way to handle async connections.
First , Create a notification like this b4 sendin the request.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateView) name:@"downloadCompleted" object:nil];
Next in the connectionDidFinishLoading method, have another notification like this,
[[NSNotificationCenter defaultCenter] postNotificationName:@"downloadCompleted" object:nil];
Have this notification code next to the line where u obtain ur dictionary data. The above notification will send a note to the first notification created, which will inturn trigger the updateView (in this example) method. You can have ur processing of obtained data in that method. Hope this helps. Cheers. :) Happy coding :)
You could also make use of a 3rd party library such as AFNetworking (block based, iOS4+, lightweight) or ASIHTTPRequest (iOS3+, hardcore).
You can initiate background calls and either provide blocks or delegates for different outcomes.
You can make use of NSThread
and NSNotifications
to get the work done asynchronously. You can check out tutorials for both on google and implement both in cumulative manner as per your requirement.
NSThread would enable you to keep your User Interface active while data is being fetched in background and then when data is downloaded it will post a notification so as to notify you that now you can fetch the data and update you User Interface accoringly.
Here are the class references :
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSThread_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
精彩评论