ios exc_bad_access because of a late dispatch - ios

[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
if(data != nil && self.superview != nil) { // <-- EXC_BAD_ACCESS on this line
[self.musicItemImg setAlpha:0.0];
[self.musicItemImg setImage:[UIImage imageWithData:data]];
[UIView animateWithDuration:0.25 animations:^{
[self.musicItemImg setAlpha:1.0];
}];
}
});
}];
Sometimes this view is removed before the async load is finished and when it tries to use self I get an EXC_BAD_ACCESS and understandably so. Can I abort an async connection? Or is there a better way to combat this?

First of all it is very bad style to start url requests in a UIView subclass. You should do that in a view controller!
The NSOperationQueue should be a class instance of the view controller. Also the views you need to configure in the completion handler must be class instances (properties). Use the same operation queue for all requests. In viewWillDisappear you can cancel all request operations with:
[self.operationQueue cancelAllOperations];

Related

UI still delayed even using GCD

In my modal UI there is a "DONE" button linked with IBAction -done:, it will upload a text to (lets say Dropbox server). Its code looks like this
- (IBAction)done:(id)sender {
// must contain text in textview
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
if (![_textView.text isEqualToString:#""]) {
// check to see if we are adding a new note
if (!self.note) {
DBFile *newNote = [[DBFile alloc] init];
newNote.root = #"dropbox";
self.note = newNote;
}
_note.contents = _textView.text;
_note.path = _filename.text;
// - UPLOAD FILE TO DROPBOX - //
NSLog(#"Initializing URL...");
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
NSURL *url = [Dropbox uploadURLForPath:self.note.path];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"PUT"];
NSData *noteContents = [self.note.contents dataUsingEncoding:NSUTF8StringEncoding];
NSLog(#"Creating session task...");
NSURLSessionUploadTask *uploadTask = [self.session uploadTaskWithRequest:request
fromData:noteContents
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse *resp = (NSHTTPURLResponse *) response;
if (!error && resp.statusCode == 200) {
NSLog(#"OK");
dispatch_async(dispatch_get_main_queue(), ^{
[self.delegate noteDetailsViewControllerDoneWithDetails:self];
});
} else {
NSLog(#"Status code: %d", resp.statusCode);
}
}];
[uploadTask resume];
});
} else {
UIAlertView *noTextAlert = [[UIAlertView alloc] initWithTitle:#"No text"
message:#"Need to enter text"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[noTextAlert show];
}
}
The delegate method noteDetailsViewControllerDoneWithDetails: of this class is look like this
-(void)noteDetailsViewControllerDoneWithDetails:(NoteDetailsViewController *)controller{
// refresh to get latest
[self dismissViewControllerAnimated:YES completion:nil];
[self notesOnDropbox];}
(notesOnDropbox is a time-consuming task). When DONE button is tapped, I expect this modal VC/UI to dismiss immediately and it fetches data on background (by notesOnDropbox method). However, when I try tapping DONE button, my UI stop responding for about seconds, after that the modal UI is dismissed. I cannot figure out where I misuse the GCD. Please help me.
if you want to dismiss your modal VC/UI immediately, just ask the delegate to dismiss,
like is:
- (IBAction)done:(id)sender {
[self.delegate noteDetailsViewControllerDoneWithDetails:self];
// ...
}
In your sample code,
you do the dismiss action after the upload task completed, but the upload task is asynchronous.
and you ask the delegate to dismiss use GCD dispatch_async, this is asynchronous task, too.
After all, you have to consider the what time to do upload, who to do upload task and what time to invoke notesOnDropbox.
First, if notesOnDropbox is a time-consuming task, then you should not be performing it on the main thread (as you are doing). If it is sufficiently time-consuming and you do it on the main thread, the WatchDog process will kill your app dead right before the user's eyes.
Second, there is no need to get off the main thread to do an upload. If you use NSURLSession correctly, it will be asynchronous.
Your code only calls noteDetailsViewControllerDoneWithDetails when the whole upload task is completed, because that's how you wrote your code. Actually, the situation seems worse. If the upload task has any kinds of problems, noteDetailsViewControllerDoneWithDetails will never be called.
You need to call noteDetailsViewControllerDoneWithDetails as soon as possible, and then think about what you are going to do when the upload fails - which might easily happen a long time later.

Nesting blocks in iOS

I'm trying to be able to call a 'completionHandler' block from inside another completionHandler block (called after an asynchronous URL request). This however results in the application crashing with the following message (I'm using Zombie objects):
*** -[CFRunLoopTimer hash]: message sent to deallocated instance
Using instruments I was able to find out that the problem is due to the block being deallocated but I can't figure out how to keep it retained for long enough. Is it because I'm calling the block from another asynchronous block? My code is below (MyCompletionHandler returns void and takes void):
-(void)requestWithRequest:(NSURLRequest*)request withCompletionHandler:(MyCompletionHandler)serverCompletionHandler{
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// do stuff…
if (serverCompletionHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
serverCompletionHandler();
});
}];
}
However this code is called through another method which supplies the serverCompletionHandler parameter (could this be the problem?).
So for example, the above method would be called by this method:
-(void)createAndSendRequestWithCompletionHandler:(MyCompletionHandler)serverCompletionHandler{
NSMutableURLRequest* request = //..
[self requestWithRequest:request withCompletionHandler:serverCompletionHandler];
}
Instruments shows that a block is either released or deleted (I assume the block I am calling) which would explain the deallocated object being called.
Any help would be appreciated.
EDIT
The code for the timer (which seems to be deallocated) is:
if ([timer isValid]) {
[timer invalidate];
}
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(createAndSendRequestWithCompletionHandler:) userInfo:nil repeats:YES];
The confusing thing is the code worked fine until I added the completionHandlers.
No thats not a problem you can acess the block within another block.I think problem is that you are already on mainQueue ([NSOperationQueue mainQueue]) and you again try to getMainQueue on mainQueue.As sendAsynchronousRequest uses NSRunloop of queue it gets deallocated when you again ask for main queue as you are already on main queue.You can check if you are already on main queue just call serverCompletionHandler else dispatch on mainqueue.You can skip this check in this case as you are sure your are main queue and can just call serverCompletionHandler()
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// do stuff…
if([[NSOperationQueue currentQueue] isEqual:[NSOperationQueue mainQueue]]){ //check for main queue
if (serverCompletionHandler) {
serverCompletionHandler();
}
}
else{
if (serverCompletionHandler) { if not than dispatch on main queue
dispatch_async(dispatch_get_main_queue(), ^{ //No need to do that
serverCompletionHandler();
});
}
}];
EDIT:Thanks of edited code.As you are using
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(createAndSendRequestWithCompletionHandler:) userInfo:nil repeats:YES];
Now as you are not passing completionhandler so by doing this createAndSendRequestWithCompletionHandler: timer passes itself to your serverCompletionHandler.
So serverCompletionHandler itself contains timer object not any block object.If you try to NSLog serverCompletionHandler in requestWithRequest you will find it is timer object.Now when dispatch_async tries to call serverCompletionHandler as it is not block it will crash.
Write these two lines in createAndSendRequestWithCompletionHandler
NSLog(#"serverCompletionHandler obnj %#",serverCompletionHandler );
NSLog(#"class %#",NSStringFromClass([serverCompletionHandler class] ));
EDIT 2
if you really want to pass the completion handler than pass in userInfo of timer object.Use below code
#import "YourViewController.h"
typedef void (^MyCompletionHandler)(void);
#interface YourViewController ()
{
NSTimer *timer;
}
#end
#implementation YourViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if ([timer isValid]) {
[timer invalidate];
}
MyCompletionHandler com = ^{
NSLog(#"Hi this is completion handler");
};
timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(createAndSendRequestWithCompletionHandler:) userInfo:#{#"serverCompletionHandler":com} repeats:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)requestWithRequest:(NSURLRequest*)request withCompletionHandler:(MyCompletionHandler)serverCompletionHandler{
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// do stuff…
if (serverCompletionHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
serverCompletionHandler();
});
}
}];
}
-(void)createAndSendRequestWithCompletionHandler:(NSTimer *)timerObj{
// NSLog(#"serverCompletionHandler obnj %#",serverCompletionHandler );
// NSLog(#"class %#",NSStringFromClass([serverCompletionHandler class] ));
//get completion handler from `userInfo`
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
[self requestWithRequest:request withCompletionHandler:timerObj.userInfo[#"serverCompletionHandler"]];
}
#end

Segue throwing NSInternalInconsistencyException, with NSURLConnection sendAsynchronousRequest

So I have been trying to create an app for a website and I've got the "Log in" page working except when it won't transition to the next view.
This is the code that I believe is causing the problem :
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(#"%#", str);
if ([str rangeOfString:#"The username or password you provided is invalid. Please try again."].location == NSNotFound) {
loginPageStatusLabel.text = #"Correct";
NSLog(#"Correct Login");
[self performSegueWithIdentifier:#"toHome" sender:self];
} else {
loginPageStatusLabel.text = #"Incorrect";
NSLog(#"Login Failed");
}
}];
* Assertion failure in -[UIKeyboardTaskQueue waitUntilAllTasksAreFinished], /SourceCache/UIKit_Sim/UIKit-2935.137/Keyboard/UIKeyboardTaskQueue.m:368
2014-05-11 00:06:51.426 LoginTests[3381:3e03] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue waitUntilAllTasksAreFinished]' may only be called from the main thread.'waitUntilAllTasksAreFinished]' may only be called from the main thread.
That is the error being thrown whenever I try to "Log in". The Segue with work if I run it alone, so I am assuming the problem is that the app is trying to go to the next View before it's ready and its causing an error.
I'm fairly new to Obj-C so if I have not posted the adequate information or not called things by the proper names please inform me.
Thank You!
I don't know what value you supplied for queue parameter, but given that your completion block is performing UI updates that must happen on the main thread, you can use [NSOperationQueue mainQueue] (or manually dispatch this code to the main queue). This queue parameter specifies what queue the completion block should be added to, and because you're doing UI related stuff in your completion block, this must be done on the main thread.
Having corrected that, if you are still have assertion errors, you can add an exception breakpoint and that will help confirm precisely where this assertion error is taking place. Or look at your stack trace.
I'd also, in addition to using [NSOperationQueue mainQueue], would suggest doing some more robust error handling:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (!data) {
// for example, no internet connection or your web server is down
NSLog(#"request failed: %#", error);
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
int statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
// for example, 404 would mean that your web site said it couldn't find the URL
// anything besides 200 means that there was some fundamental web server error
NSLog(#"request resulted in statusCode of %d", statusCode);
return;
}
}
// if we got here, we know the request was sent and processed by the web server, so now
// let's see if the login was successful.
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// I'm looking for "Welcome" ... I doubt that's right (but I don't have access to
// your web server, so I'm guessing). But the idea is that you have to find whatever
// appears after successful login that is not in the response if login failed
if ([responseString rangeOfString:#"Welcome"].location != NSNotFound) {
loginPageStatusLabel.text = #"Correct";
NSLog(#"Correct Login");
[self performSegueWithIdentifier:#"toHome" sender:self];
} else {
loginPageStatusLabel.text = #"Incorrect";
NSLog(#"Login Failed");
}
}];

NSURLSessionDataTask and handler block

I'm developing a library that gets json data from a server, and I'm using NSURLSessionDataTask. In order to test my library I created a new project that calls this library method.
typedef void (^CompletionBlock)();
// More stuff...
- (void)downloadAllPodcastsMetadataWithCompletionHandler:(CompletionBlock)block{
NSURL *url = [NSURL URLWithString:#"MyServerURL"];
NSURLSessionDataTask *dataTask = [self.session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Connection and response handling stuff...
// When my data is saved, executes the block parameter
dispatch_async(dispatch_get_main_queue(), ^{
block();
});
}
[dataTask resume];
}
EDIT: I forgot to put [dataTask resume] here, but it's in my code. Sorry.
EDIT 2: So, as some of you have said, i changed dispatch_sync with dispatch_async. But the result is the same :(
In my test project i call this method like this.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[manager downloadAllPodcastsMetadataWithCompletionHandler:^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
But the network activity Indicator never shows off. It's like the block parameter never executes.
It's because it executes the NSURLSessionDataTask logic inside a library and then I should use something else instead dispatch_sync?
I already checked NSURLSessionDataTask not executing the completion handler block and I think I do the same. If it helps, manager is a Singleton. Any thoughts?
Thank you.
You need to start the download with [dataTask resume];.
You need to add [dataTask resume]; and also add this in completion handler
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
I tested some code.
Dispatch_sync won't work.
dispatch_sync(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
Dispatch_async worked for me. But I had to put the code on veiwWillAppear.
Not sure what's going to happen using it inside a block...
dispatch_async(dispatch_get_main_queue(), ^{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
});
Well, i'm dumb.
There was an error in my previous code i didn't notice, so the completion block war never executing. That is what happens when someone has too much confidence in his code.
Sorry for bothering you.

Make multiple NSURLConnections and using sendAsynchronousRequest:queue:completionHandler: iOS 5 method

I have some difficulties to set up the correct configuration relative to sendAsynchronousRequest:queue:completionHandler: method (NSURLConnection class).
My scenario is the following:
I set up a singleton class that manages different NSURLConnections. This singleton istance has a NSOperation Queue (called downloadQueue) that makes a request to a web server and retrieves a string path (1).
Once done, the path is used to download a file within a web server (2). Finally, when the file has been correctly downloaded, I need to update the UI (3).
I figured out only the first request: the one through which I'm able to download the path. Could you suggest me a way to perform the other two steps?
Few questions here:
the download queue (downloadQueue) is not the main one, is it possible to open a new NSURLConnection in that queue? In other words, is it correct? (See comments in code snippets)
if the previous question is correct, how can I grab the main queue and update the UI?
Here the code snippet I use to perform the first step where downloadQueue is an instance variable that can be obtain through accessor mehods (#property/#synthesized);
// initializing the queue...
downloadQueue = [[NSOperation alloc] init];
// other code here...
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[self downloadQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if([data length] > 0 && error == nil) {
// here the path (1)
// how to perform a second connection?
// what type of queue do I have to use?
}
}];
You're on the right track for performing your first download.
In the completion handler block after the first download, you're computing the URL that you'll need for a second download, right? Then you can perform that second download the same way: call +[NSURLConnection sendAsynchronousRequest:...] again with the new URL and the same queue. You can do this within the completion block for the first download.
To update the UI after the second download is done, switch to the main queue within the completion block for that download. You can do this with dispatch_async() or dispatch_sync() (in this case it doesn't matter which because you don't have further work to do on the download queue) and dispatch_get_main_queue(), or with -[NSOperationQueue addOperationWithBlock:] and +[NSOperationQueue mainQueue].
Your code should look something like this:
// init download queue
downloadQueue = [[NSOperationQueue alloc] init];
// (1) first download to determine URL for second
[NSURLConnection sendAsynchronousRequest:urlRequest queue:[self downloadQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if([data length] > 0 && error == nil) {
// set newURLRequest to something you get from the data, then...
// (2) second download
[NSURLConnection sendAsynchronousRequest:newURLRequest queue:[self downloadQueue] completionHandler:^(NSURLResponse *newResponse, NSData *newData, NSError *newError) {
if([newData length] > 0 && newError == nil) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// (3) update UI
}];
}
}];
}
}];
For updating the ui, as far as I know, you have to do that on the main thread. The ui could be updated from other threads but those updates are not fully reliable. In an app that I put together that made request to a web service, I make use of dispatch_async() to get access to the main queue and then I update, in my case a table view, from that call.
dispatch_async(dispatch_get_main_queue(), ^{
//block to be run on the main thread
[self.tableView reloadData];
});
I hope this helps.

Resources