Eman87
Eman87

Reputation: 2745

can't show UIAlertView in iPhone although I added show

I wrote this code to define the UIAlertView

-(void)showAlertMethod2 {
progressAlert2 = [[UIAlertView alloc] initWithTitle:@"تتم المزامنة..يرجى الانتظار ...\n" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
CGRect alertFrame = progressAlert2.frame;
UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicator.frame = CGRectMake(135,alertFrame.size.height+55, alertFrame.size.width,30);
activityIndicator.hidden = NO;
activityIndicator.contentMode = UIViewContentModeCenter;
[activityIndicator startAnimating];
[progressAlert2 addSubview:activityIndicator];
[progressAlert2 show];
}

-(void)dismissAlertMethod2
{
[progressAlert2 dismissWithClickedButtonIndex:0 animated:YES];
}

And I'm Calling here:

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

[self performSelector:@selector(syncToServer) withObject:nil];
[self performSelector:@selector(syncFromServer) withObject:nil]

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

syncToServer is a method to synchronize data to server and syncFromServer is to synchronize data to server

the problem is that the UIAlertView not show, anyone know what's missed? Thanks in advance.

Upvotes: 0

Views: 186

Answers (3)

M.Alatrash
M.Alatrash

Reputation: 1265

you are using the NSThread detachNewThreadSelector: to Show the UIAlertView also to dismiss your UIAlertView, the thing is you now have 2 separate NSThreads one for showing the alert and the other to dismiss it. So you can't figure which Thread will finish first. so maybe the the second Thread wich dismiss the UIAlertView finishes first, which will prevent the UIAlertView to Show.

try to call the UIAlertView without creating new Thread for each method.

[self showAlertMethod2];
[self dismissAlertMethod2];

Upvotes: 1

Eman87
Eman87

Reputation: 2745

I solved by the following code

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self showAlertMethod2];
                });

                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncToServer) withObject:nil];
                [self performSelector:@selector(syncFromServer) withObject:nil];

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self dismissAlertMethod2];
                });

Upvotes: 2

Ashim
Ashim

Reputation: 286

-(void)showAlertMethod2 {
    progressAlert2 = [[UIAlertView alloc] initWithTitle:@"تتم المزامنة..يرجى الانتظار ...\n" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    CGRect alertFrame = progressAlert2.frame;
    UIActivityIndicatorView* activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicator.frame = CGRectMake(135,alertFrame.size.height+55,    alertFrame.size.width,30);
    activityIndicator.hidden = NO;
    activityIndicator.contentMode = UIViewContentModeCenter;
    [activityIndicator startAnimating];
    [progressAlert2 addSubview:activityIndicator];
    [progressAlert2 show];
}

-(void)dismissAlertMethod2
{
    [progressAlert2 dismissWithClickedButtonIndex:0 animated:YES];
}

This is your method just fine.

Now call them like

-(void)syncBothWay{
    [self showAlertMethod2];
    [self performSelector:@selector(syncToServer) withObject:nil afterDelay:0.0];
    [self performSelector:@selector(syncFromServer) withObject:nil afterDelay:0.0];
}

-(void)syncToServer{
    //sync
}

-(void)syncFromServer{
    //sync
    after sync completed call [self dismissAlertMethod2];
}

Upvotes: 0

Related Questions