user2836305
user2836305

Reputation: 31

UIAlertView crash iOS7 - Assertion failure

I have a problem regarding the UIAlertView on iOS7. When I launch my application, it crashes with the following message:

*** Assertion failure in -[UIKeyboardTaskQueue performTask:], /SourceCache/UIKit_Sim/UIKit-2903.2/Keyboard/UIKeyboardTaskQueue.m:388

The error occurs on the following line:

- (IBAction)updatePositions:(id)sender{
     _alert = [[UIAlertView alloc] initWithTitle:@"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
     [_alert show];     <====== IT CRASHS HERE
     [NSThread detachNewThreadSelector:@selector(updateDataThread) toTarget:self withObject:nil];
}

I'm using ARC and the property _alert is set defined as: @property (nonatomic,strong)

This error seems strange, because on iOS6 the code works perfectly and I don't know what should be different on iOS7.

Does anyone have an idea what could the error?

Thanks in advance.

Upvotes: 3

Views: 3455

Answers (6)

laura
laura

Reputation: 491

You can also do like this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your title" message:@"Your message" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];

However, if you need to show the same alert in multiple places it's better to create a separate function for it.

Upvotes: 0

Francis F
Francis F

Reputation: 3285

Put your alertview code in a separate function like

-(void)showAlert
{
  _alert = [[UIAlertView alloc] initWithTitle:@"text" message:nil delegate:self    cancelButtonTitle:nil otherButtonTitles: nil];
   [_alert show];
}

Then in your IBAction do this

- (IBAction)updatePositions:(id)sender
{
  [self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:YES];

  [NSThread detachNewThreadSelector:@selector(updateDataThread) toTarget:self withObject:nil];
}

Upvotes: 0

tjdaniel2
tjdaniel2

Reputation: 181

I had the same problem as well but not too familiar with the method dispatch_async. I used

[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];

and the problem hasn't come up again.

Upvotes: 0

Pelpotronic
Pelpotronic

Reputation: 560

I encountered the same error, and the issue was that the UIAlertView was attempting to be shown from a thread which wasn't the main thread.

The crash however wouldn't always occur, only when a first AlertView was already being shown while this second AlertView was trying to pop up as well.

In my case, a simple fix was to do:

    //Your code here
    ...

    //Alert
    _alert = [[UIAlertView alloc] initWithTitle:@"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];

    dispatch_async(dispatch_get_main_queue(), ^{
       //Show alert here
       [_alert show];
       });

    //Resume your code here
    ...

Upvotes: 8

ambulephabus
ambulephabus

Reputation: 21

I just had this problem after forgetting that I was working from a background thread. I don't know if that's the case here, but I'd make sure you're not trying to call updatePositions: from anything other than the main thread.

Upvotes: 2

Jordan Montel
Jordan Montel

Reputation: 8247

Change your code like this :

_alert = [[UIAlertView alloc] initWithTitle:@"text" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[_alert show];

removing [[ and ]] around @"text"

But, your I don't think your problem came from this UIAlertView.

Upvotes: 0

Related Questions