can Mainqueue do background task while using in NSURLConnection? - ios

1)When we use mainQueue in NSURLConnection for sendasynchronousrequest ,How it gets response through running in background?because mainqueue is for updating UI only.For EX:
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
[delegate receivedData:data];
else if ([data length] == 0 && error == nil)
[delegate emptyReply];
else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
[delegate timedOut];
else if (error != nil)
[delegate downloadError:error];
}];

When you say mainQueue is for updating UI only is not strictly right.
The exact point is that mainQueue is used to update UI and so developers avoid to do any other tasks on the mainQueue to no introduce delays on the UI. It's not completely forbidden.
But, doing asynchronous task like a network request on the mainQueue is really a bad idea because it will introduce an unbounded delay that will completely break the smoothness of your UI
To not block your mainQueue you should execute your request this way :
Create your own application operation queue
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
then reuse your operationQueue anywhere you need it to execute your network requests
[NSURLConnection sendAsynchronousRequest:urlRequest queue:operationQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil)
[delegate receivedData:data];
else if ([data length] == 0 && error == nil)
[delegate emptyReply];
else if (error != nil && error.code == ERROR_CODE_TIMEOUT)
[delegate timedOut];
else if (error != nil)
[delegate downloadError:error]; }];
Another alternative, safer and more effective is to use AFNetworking framework

Related

Objective-C: Wait to execute 'While' loop until NSURLConnection request is complete

Basically I want a way to issue a NSURLRequest multiple times in a loop until a certain condition has been met. I am using a rest api but the rest api only allows up to a maximum of 1,000 results at a time. So if i have, lets say 1,500 total, i want to make a request to get the first 1,000 then i need to get the rest with another almost exact request , except the startAt: parameter is different(so i could go from 1001 - 1500. I want to set this up in a while loop(while i am done loading all the data) and am just reading about semaphores but its not working out like I expected it to. I don't know how many results I have until i make the first request. It could be 50, 1000, or 10,000.
here is the code:
while(!finishedLoadingAllData){
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
NSURLRequest *myRequest = [self loadData: startAt:startAt maxResults:maxResults];
[NSURLConnection sendAsynchronousRequest:myRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(error){
completionHandler(issuesWithProjectData, error);
}
else{
NSDictionary *issuesDictionary = [[NSDictionary alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]];
[issuesWithProjectData addObjectsFromArray:issuesDictionary[#"issues"]];
if(issuesWithProjectData.count == [issuesDictionary[#"total"] integerValue]){
completionHandler([issuesWithProjectData copy], error);
finishedLoadingAllData = YES;
}
else{
startAt = maxResults + 1;
maxResults = maxResults + 1000;
}
}
dispatch_semaphore_signal(semaphore);
}];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
Basically I want to keep the while loop waiting until the completion block finished. Then and only then do i want the while loop to check if we have all of the data or not(and if not, make another request with the updated startAt value/maxResults value.
Right now it just hangs on dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
What am i doing wrong or what do i need to do? Maybe semaphores are the wrong solution. thanks.
Ok. The more I look, the more I don't think its a bad idea to have semaphores to solve this problem, since the other way would be to have a serial queue, etc. and this solution isn't all that more complicated.
The problem is, you are requesting the completion handler to be run on the main thread
[NSURLConnection sendAsynchronousRequest:myRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
and you are probably creating the NSURL request in the main thread. Hence while it waits for the semaphore to be released on the mainthread, the NSURL completion handler is waiting for the mainthread to be free of its current run loop. So create a new operation queue.
would it not be easier to do something like this instead:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //run on a background thread
while(!finishedLoadingAllData){
NSURLRequest *myRequest = [self loadData: startAt:startAt maxResults:maxResults];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error]; //blocks until completed
if(response.statusCode == 200 && responseData != nil){ //handle response and set finishedLoadingAllData when you want
//do stuff with response
dispatch_sync(dispatch_get_main_queue(), ^{
//do stuff on the main thread that needs to be done
}
}
});
Please dont do that.. NSURLConnection sendAsynchronousRequest will be loading itself in loop for you, if your data is in chunk.. try this instead..
__block NSMutableData *fragmentData = [NSMutableData data];
[[NSOperationQueue mainQueue] cancelAllOperations];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
[fragmentData appendData:data];
if ([data length] == 0 && error == nil)
{
NSLog(#"No response from server");
}
else if (error != nil && error.code == NSURLErrorTimedOut)
{
NSLog(#"Request time out");
}
else if (error != nil)
{
NSLog(#"Unexpected error occur: %#", error.localizedDescription);
}
else if ([data length] > 0 && error == nil)
{
if ([fragmentData length] == [response expectedContentLength])
{
// finished loading all your data
}
}
}];
I've created two chunky json response from server handling method.. And one of them is this, so hope this will be useful to you as well.. Cheers!! ;)

Set UILabel text within code block

I have a code block for my NSURLConnection sendAsynchronousRequest, and when I try to set a UILabel's text, the text is never set, even though the values are there. Here's my code:
NSString *address = [addresses objectAtIndex:indexPath.row];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://myurl.com/%#", address]]];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length] > 0 && error == nil)
{
NSString *dataOfData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(![dataOfData isEqualToString:#"ERROR: address invalid"]) {
[balanceLabel setText:[NSString stringWithFormat:#"Balance: %#", dataOfData]];
if(data) {
qrCodeButton.alpha = 1;
}
} else {
errorLabel.text = #"This address is invalid.";
}
}
else if ([data length] == 0 && error == nil)
{
NSLog(#"Nothing was downloaded.");
[balanceLabel setText:#"Server Error, Please Try Again"];
}
else if (error != nil){
NSLog(#"Error = %#", error);
}
}];
Why is the UILabel's text never set? Is there a limitation to code blocks? If so, how would I fix my problem? Cheers!
It is because an NSOperationQueue is not the main thread. What you're doing is illegal. And the sad thing is that there is no need for it! Just say:
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// ... and the rest exactly as you have it now
All fixed. Your request is asynchronous on a background thread, but when it comes back to you on the completion handler, you'll be on the main thread and able to talk to the interface etc.
Your code operates UI element should execute on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
if ([data length] > 0 && error == nil)
{
NSString *dataOfData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if(![dataOfData isEqualToString:#"ERROR: address invalid"]) {
[balanceLabel setText:[NSString stringWithFormat:#"Balance: %#", dataOfData]];
if(data) {
qrCodeButton.alpha = 1;
}
} else {
errorLabel.text = #"This address is invalid.";
}
}
else if ([data length] == 0 && error == nil)
{
NSLog(#"Nothing was downloaded.");
[balanceLabel setText:#"Server Error, Please Try Again"];
}
else if (error != nil){
NSLog(#"Error = %#", error);
}
}) ;
Make sure errorLabel is not nil and the UILabel is visible (It is added in the view hierarchy and its frame is appropriate).

iOS Downloading image in background and updating the UI

I am trying update an imageview with new image for every 1 sec by downloading the image from the server. The downloading happens in background thread. Below shown is the code
NSTimer *timer = [NSTimer timerWithTimeInterval:0.5
target:self
selector:#selector(timerFired:)
userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
-(void)timerFired:(id)sender
{
NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL
URLWithString:#"http://192.168.4.116:8080/RMC/ImageWithCommentData.jpg"]];
NSOperationQueue *queueTmp = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queueTmp completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
{
[self performSelectorOnMainThread:#selector(processImageData:) withObject:data waitUntilDone:TRUE modes:nil];
}
else if ([data length] == 0 && error == nil)
{
}
else if (error != nil)
{
}
}];
}
-(void)processImageData:(NSData*)imageData
{
NSLog(#"data downloaded");
[self.hmiImageView setImage:[UIImage imageWithData:imageData]];
}
My image is getting downloaded. But ProcessImageData method is not called. Please help me to fix this issue.
The problem is you are calling NSURLConnection asynchronously :
[NSURLConnection sendAsynchronousRequest:request queue:queueTmp completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
so before you get any data the : if ([data length] > 0 && error == nil) get called .
so the data length remains 0 thats why your method is not called . for achieving your requirement you can do like :
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = //download image here
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
//set image here
}
});
}
});

Retain cycle on an Obj-C block using itself

I have a block to use as a completionHandler for an NSURLConnection asynchronous request whose main job is to spawn a new asynchronous request using the same block for the new requests completion handler. I am doing this because it effectively solves another problem which is to line up a sequence of asynchronous calls and have them fired off in the background. This is working marvelously for us, but we have a warning I am concerned about. Namely, XCode thinks I have a retain cycle. Perhaps I do, I don't know. I've been trying to learn about blocks over the last couple hours but I haven't found an explanation for recursive uses like mine. The warning states `Block will be retained by the captured object'.
My best guess so far is that a retain cycle is exactly what we want, and that to clear when we are done, we just nillify the block variable, which I'm doing. It doesn't get rid of the error, but I don't mind as long as I'm not leaking memory or doing some black magic I'm not aware of. Can anyone address this? Am I handling it right? If not, what should I be doing?
void (^ __block handler)(NSURLResponse *, NSData *, NSError*);
handler = ^(NSURLResponse *response, NSData *data, NSError *error)
{
[dataArray addObject:data];
if (++currentRequestIndex < [requestsArray count])
{
if (error)
{
[delegate requestsProcessWithIdentifier:_identifier processStoppedOnRequestNumber:currentRequestIndex-1 withError:error];
return;
}
[delegate requestsProcessWithIdentifier:_identifier completedRequestNumber:currentRequestIndex-1]; // completed previous request
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:currentRequestIndex]
queue:[NSOperationQueue mainQueue]
completionHandler:handler]; // HERE IS THE WARNING
}
else
{
[delegate requestsProcessWithIdentifier:_identifier completedWithData:dataArray];
handler = nil;
}
};
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:0]
queue:[NSOperationQueue mainQueue]
completionHandler:handler];
Try to store your handler block into an instance variable of your view controller (or whatever class you're in).
Assuming that you declare an instance variable named _hander:
{
void (^_handler)(NSURLResponse *, NSData *, NSError*);
}
Change your code to:
__weak __typeof(&*self)weakSelf = self;
_handler = ^(NSURLResponse *response, NSData *data, NSError *error)
{
[dataArray addObject:data];
if (++currentRequestIndex < [requestsArray count])
{
if (error)
{
[delegate requestsProcessWithIdentifier:_identifier processStoppedOnRequestNumber:currentRequestIndex-1 withError:error];
return;
}
[delegate requestsProcessWithIdentifier:_identifier completedRequestNumber:currentRequestIndex-1]; // completed previous request
__strong __typeof(&*self)strongSelf = weakSelf;
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:currentRequestIndex]
queue:[NSOperationQueue mainQueue]
completionHandler:strongSelf->_handler];
}
else
{
[delegate requestsProcessWithIdentifier:_identifier completedWithData:dataArray];
}
};
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:0]
queue:[NSOperationQueue mainQueue]
completionHandler:_handler];
void (^handler)(NSURLResponse *, NSData *, NSError*);
typeof(handler) __block __weak weakHandler;
weakHandler = handler = ^(NSURLResponse *response, NSData *data, NSError *error)
{
[dataArray addObject:data];
if (++currentRequestIndex < [requestsArray count])
{
if (error)
{
[delegate requestsProcessWithIdentifier:_identifier processStoppedOnRequestNumber:currentRequestIndex-1 withError:error];
return;
}
[delegate requestsProcessWithIdentifier:_identifier completedRequestNumber:currentRequestIndex-1]; // completed previous request
[NSURLConnection sendAsynchronousRequest:[requestsArray objectAtIndex:currentRequestIndex]
queue:[NSOperationQueue mainQueue]
completionHandler:weakHandler]; // HERE IS THE WARNING
}
else
{
[delegate requestsProcessWithIdentifier:_identifier completedWithData:dataArray];
}
};

NSURLConnection sendAsynchronousRequest completionHandler logs appearing before updating the UI

I have this code to send a json string to a server
[NSURLConnection
sendAsynchronousRequest:req
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
NSLog(#"Done");
}
else if ([data length] == 0 && error == nil)
{
NSLog(#"Nothing was downloaded.");
self.resultLabel.text=#"Done!";
self.view.userInteractionEnabled = TRUE;
}
else if (error != nil){
NSLog(#"Error = %#", error);
}
}];
The asynchronous request finishes fine and the logs show up almost immediately after it is finished. However, this code:
self.resultLabel.text=#"Done!";
self.view.userInteractionEnabled = TRUE;
Takes a good 10 seconds to show up in the UI. Anyone know why this would happen?
You must perform all UI changes in main thread:
....
if ([data length] == 0 && error == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
self.resultLabel.text=#"Done!";
self.view.userInteractionEnabled = TRUE;
});
}
....

Resources