2

AFNetworking用来发出网络请求。Web 请求完成后。我希望显示 UIAlertView。我正在使用 ARC,代码似乎可以在设备上运行。如果我使用模拟器,我会收到错误:EXC_BAD_ACCESS

我究竟做错了什么?

UIAlertView* postSubmitAlertView = [[UIAlertView alloc] init];
postSubmitAlertView.delegate = self;
[postSubmitAlertView addButtonWithTitle:@"Ok"];

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] 
    initWithRequest:request];

[op setCompletionBlock:^(void) {
    if(op.response.statusCode == 200) {

        postSubmitAlertView.title = @"Submission Good";
        postSubmitAlertView.message = @"GOOD";

   } else {

        postSubmitAlertView.title = @"Submission Failed.";
        postSubmitAlertView.message = @"FAIL";
   }

   [postSubmitAlertView show];
}];
[op start];
4

3 回答 3

4

关键问题是UIKit应该在主线程上调用东西。

注意:在大多数情况下,UIKit 类只能在应用程序的主线程中使用。对于从 UIResponder 派生的类或涉及以任何方式操作应用程序的用户界面的类尤其如此。

UIKit 框架参考

查看NSOperation的文档setCompletionBlock:

讨论 无法保证完成块的确切执行上下文,但通常是辅助线程。因此,您不应使用此块来执行任何需要非常特定的执行上下文的工作。相反,您应该将该工作分流到应用程序的主线程或能够执行此操作的特定线程。

修改代码最简单的解决方案是UIKit在主线程上调用这些东西

- (void)viewDidLoad
{
  [super viewDidLoad];

  NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];
  NSURLRequest *request = [NSURLRequest requestWithURL:URL];

  AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc]
                                initWithRequest:request];

  [op setCompletionBlock:^(void) {

    dispatch_async(dispatch_get_main_queue(), ^{
      UIAlertView* postSubmitAlertView = [[UIAlertView alloc] init];
      postSubmitAlertView.delegate = self;
      [postSubmitAlertView addButtonWithTitle:@"Ok"];

      if(op.response.statusCode == 200) {

        postSubmitAlertView.title = @"Submission Good";
        postSubmitAlertView.message = @"GOOD";

      } else {

        postSubmitAlertView.title = @"Submission Failed.";
        postSubmitAlertView.message = @"FAIL";

      }

      [postSubmitAlertView show];
    });

  }];
  [op start];
}
于 2012-10-05T21:08:18.840 回答
1

EXC_BAD_ACCESS 是由访问已释放的对象引起的。为避免这种情况,请调用 UIAlertView 类型的模式:

//Function body:

-(void)checkSaving
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Do you want to add these results to your database?"
                                                    message:@"\n\n"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Save", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];

    //this prevent the ARC to clean up :
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    NSDate *d;
    d = (NSDate*)[d init];
    while ([alert isVisible]) {
        [rl runUntilDate:d];
    }
}

//Your choice result:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 1)//Save
    {
        //do something    
    }
    if (buttonIndex == 0)//NO
    {
        //do something
    }
}

//Register the functions in the interface declaration:

@interface yourViewController ()
-(void)checkSaving
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
@end


//To call:

[self checkSaving];

我希望这会对你有所帮助。

于 2012-12-08T13:01:16.237 回答
0
UIAlertView* postSubmitAlertView = [[UIAlertView alloc] init];
postSubmitAlertView.delegate = self;
[postSubmitAlertView addButtonWithTitle:@"Ok"];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"success: %@", operation.responseString);   
        postSubmitAlertView.title = @"Submission Good";
        postSubmitAlertView.message = @"GOOD";

} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);
        postSubmitAlertView.title = @"Submission Failed.";
        postSubmitAlertView.message = @"FAIL";

}
 ];

[postSubmitAlertView show];

希望这能解决您的问题。

于 2012-10-05T21:03:33.707 回答