I have seen a lot of tutorials about nsurl asynchronous. I followed those tutorials and implement following.
-(id) connect_asych:(NSDictionary *)input page:(NSString *)page{
NSString* urlString= [@"*s.com/music/" stringByAppendingString:page];
NSURL *url = [NSURL URLWithString:urlString];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:@"POST"];
//initialize a post data
NSString *post = [self convert:input];
//set request content type we MUST set this value.
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
//set post data of request
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSHTTPURLResponse *responseCode = nil;
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error1) {
if(error !=nil){
_responseData=nil;
}
[_responseData appendData: data];
NSLog(@"%@",_responseData);
}];
id object = [NSJSONSerialization JSONObjectWithData:_responseData options:NSJSONReadingAllowFragments error:&error];
if(error !=nil){
_error=[[NSString alloc] initWithFormat:@"error"];
return error;
}
return object;
}
if my viewdidload, I called the above method.
I got the data from the database successfully by using synchronous method. The problem is when I used asynchronous method, I could not get data. Should I call asynchronous method in the viewdidload?