Pregunta

I have an iPad app that makes appointments. If the user happens to overlap an existing appointment, I need to let them know via a UIAlertView. The problem is of course, that the UIAlertView doesn't get displayed until the 'Save' method completes it's processing.

I was thinking of using a separate thread (call it 'B') for displaying the alert, and passing the button that was tapped back to the main thread (call it 'A'). My way of doing this was to have the main thread ('A') call another method, which would create the thread ('B'), show the alert on the new thread ('B') and return to the main thread ('A') after the user has tapped a button on the alert, returning some value indicating which button was tapped.

I was hoping that since I placed the thread creation in a separate method, the calling method would wait for it to return before continuing it's processing in the main thread ('A').

Is this feasible?

UPDATE

I just tried this, and it didn't work (alert was displayed way after the processing has completed and the main thread continued processing -- not what I wanted!):

    if(overlapFlag == [NSNumber numberWithInt:1])  {  //  there IS an overlap

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{  //Here your non-main thread.

        NSLog (@"Hi, I'm new thread");

        UIAlertView *testView = [UIAlertView alertViewWithTitle:@"Warning!" message:@"This appointment overlaps an existing appointment.  Tap Continue to save it or Cancel to create a new appointment."];
        [testView addButtonWithTitle:@"Continue" handler:^{ NSLog(@"Yay!"); }];
        [testView addButtonWithTitle:@"Cancel" handler:^{ [self reloadAppointmentList]; }];
        [testView show];

        dispatch_async(dispatch_get_main_queue(), ^{   //Here you returns to main thread.

            NSLog (@"Hi, I'm main thread");
        });

    });

}
¿Fue útil?

Solución

This is possible.

But working with UI in the background thread isn't good practice. Why don't you show UIAlertView before starting the 'Save' method, and call the 'Save' method in another thread? For example, this would be a better solution:

- (void) someSaveMethod{
    [self showAlertView];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE _PRIORITY_DEFAULT, 0), ^(){
        // call you save method
        // after end save method call:
        dispatch_async(dispatch_get_main_queue(), ^{
            [self hideAllertView];
        });
     });
 }

Otros consejos

No need to use separate threads. Break your save process up into everything you can process before finding out about overlap and the rest of the save process.

- (void)saveBegin
{
   // start save process
   // ...
   // now check for overlap
   if(overlapFlag == @1)  
   {  //  there IS an overlap

      NSString *message = @"alert message goes here"; 
      UIAlertView *testView = [UIAlertView alertViewWithTitle:@"Warning!" 
                                                      message:message];

      [testView addButtonWithTitle:@"Continue" 
                           handler:^{ [self saveComplete]; }];

      [testView addButtonWithTitle:@"Cancel" 
                           handler:^{ [self reloadAppointmentList]; }];
      [testView show];

    }
    else
    {
       [self saveComplete];
    }

}

- (void)saveComplete
{
    // complete save process
    // .. 
} 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top