Invoke model method with block that will run on the main thread

前端 未结 4 2037
野性不改
野性不改 2020-12-13 16:36

one of the central tenets of the architecture of my latest app is that I\'m going to call methods on the app\'s model which will be async and accept failure and success scen

4条回答
  •  眼角桃花
    2020-12-13 17:12

    Similar approach works also with NSOperationQueue:

    NSBlockOperation *aOperation = [NSBlockOperation blockOperationWithBlock:^ 
     {  
        if ( status == FAILURE )
        { 
            // Show alert -> make sure it runs on the main thread
            [[NSOperationQueue mainQueue] addOperationWithBlock:^ 
             {
                 UIAlertView    *alert = [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Your action failed!" delegate:nil 
                                          cancelButtonTitle:@"Ok" otherButtonTitles:nil] autorelease];
                 [alert show];
             }];
        }
     }];
    
    // myAsyncOperationQueue is created somewhere else
    [myAsyncOperationQueue addOperation:aOperation];
    

提交回复
热议问题