asihttprequest


Get return value within an ASIBasicBlock


I have a view controller which will make a server request. I have put all server request handlers in a single class. So in the view controller, just use [Apihandler getlist] to get list for example.
In class Apihandler, I used block in
[_request setCompletionBlock:^{}]
to get server response (JSON object).
If JSON object contains a key/value named "error", I will take it as failure though it is complete to ASIHttpRequest itself. Otherwise, it is success.
The question is: how do I get the return value of above completion block? I checked ASI document, ASIBasicBlock is void return type.
I did this with a delegate. So I have code that looks like this:
[request setCompletionBlock:^{
NSLog(#"Data retrieved");
NSData *data = [request responseData];
NSLog(#"Status Code: %d", [request responseStatusCode]);
dispatch_async(backgroundQueue, ^(void){
[self processData:data];
});
}];
Which basically says when this completion block runs, call the processData message in the background. In the processData message I make use a delegate to return data to my main UI thread. That message looks like this:
- (void)processData:(NSData *)data {
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.delegate requestDataReady:json];
});
}
I hope that helps.
UPDATE:
Of course you don't have to run the process method in a background thread. You could just as easily call [self processData:data] within the block or even place the code from processData inside of the block.

Related Links

Downloading data using ASIHttpRequest
ASINetworkQueue without setting DownloadDestinationPath
I am using ASIHTTPREquest I am geeting some issue in MAc OSx xcode 3.1
What to do now that asihttprequest's owner stopped working on the project?
ASIHttPRequest didReceiveData- how to populate responseData, responseString?
RestKit with ASIHTTPRequest
ASIHTTPRequest backed RestKit object mapping
ASIHTTPRequest begins but never ends
MBProgressHUD with ASIHTTPRequest
ASIHTTPRequest and ASINetworkQueue and JSON Parsing
iOS 5 Hanging on ASIHTTPRequests
ASIHTTPRequest authentication needed when not on WiFi, with credentials given
Get return value within an ASIBasicBlock
ASIHTTPRequest setDownloadDestinationPath crashing
ASIHTTPRequestErrorDomain Code=4 “The request was cancelled”
ASIHttpRequest send a weired status code of zero. Not reaching the server

Categories

HOME
openshift
visual-studio
firebase
verification
shinyapps
cucumber
bokeh
devexpress
stacktrace.js
braintree
adobe
typeerror
google-webmaster-tools
hyperledger
anchor
parsley.js
distributed-computing
temperature
reselect
vertica
scala-ide
nose
codefluent
jitsi
neo4j.rb
animated-gif
managed-c++
introspection
annotation-processing
fatfs
docx4j
infopath2010
cmis
bootstrap-sass
ftp-server
ms-dos
llvm-ir
flowchart
right-click
grails-spring-security
yoast
jqgrid-asp.net
type-inference
comm
amazon-elastic-beanstalk
redisson
android-webservice
stateless-session-bean
connect-direct
glimpse
rustdoc
ogg
mifos
ternary-operator
ftp-client
callouts
jsr363
ioctl
miniconda
mousehover
nette
enaml
360-degrees
search-form
roxygen2
darwin
com0com
scala-swing
sablecc
famo.us
juttle
flurry-analytics
csslint
preorder
webaii
jack
progress-db
hints
xs
jquery-lazyload
django-filebrowser
cyrillic
wndproc
uitextfielddelegate
quartz-core
glx
groovy-console
kernel32
kohana-auth
photo-management
css-friendly
ognl
dip
channelfactory
bass
resharper-5.0
bindable-linq

Resources

Encrypt Message