Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
服务器错误的iOS SDK全局错误处理程序_Ios_Asynchronous_Error Handling_Afhttprequestoperation - Fatal编程技术网

服务器错误的iOS SDK全局错误处理程序

服务器错误的iOS SDK全局错误处理程序,ios,asynchronous,error-handling,afhttprequestoperation,Ios,Asynchronous,Error Handling,Afhttprequestoperation,我有一个叫做Request的类。请求是异步的。在请求结束时,我检查响应是否存在服务器错误。如果出现服务器错误,我将发出通知 +(BOOL)checkForResponseError:(NSArray*)response{ if ([[response objectAtIndex:1] boolValue] == YES) { [[NSNotificationCenter defaultCenter] postNotificationName:@"Server Error"

我有一个叫做Request的类。请求是异步的。在请求结束时,我检查响应是否存在服务器错误。如果出现服务器错误,我将发出通知

+(BOOL)checkForResponseError:(NSArray*)response{
    if ([[response objectAtIndex:1] boolValue] == YES) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Server Error" object:nil userInfo:@{@"error":response[0]}];
        NSLog(@"Server Response Error %@", response[0]);
        return YES;
        //[NSException raise:@"Server error on response" format:@"Response error: %@", response[0]];
    }else if(response == nil){
        NSLog(@"nil response");
        #ifdef DEBUG
            [NSException raise:@"Error 500" format:@"Check the logs for more information"];
        #endif
        return YES;
    }
    return NO;
}

+(Request*)request{
Request *request = [[Request alloc] init];

request.success = ^(AFHTTPRequestOperation *operation, id responseObj) {
    NSLog(@"Success on operation %@ with result %@", [operation.request.URL absoluteString], operation.responseString);
    NSArray *result = responseObj;
    if (![Request checkForResponseError:result]){
        request.completion(result);
    }
};

request.failure = ^(AFHTTPRequestOperation *operation, NSError *error){
    NSLog(@"Error: %@, result %@", error.localizedDescription, operation.responseString);
    #ifdef DEBUG
        [NSException raise:@"action failed" format:@"Link %@ Failed with objective c error: %@", [operation.request.URL absoluteString], error];
    #endif
};

return request;
}

-(AFHTTPRequestOperationManager*)getWithAction:(NSString *)action parameters:(NSDictionary*)params success:(void (^)(NSArray*))c{
    NSLog(@"Getting with action %@ and params %@", action, params);

    completion = c;
    AFHTTPRequestOperationManager *manager = [Header requestOperationManager];

    [manager GET:[NSString stringWithFormat:@"%@%@", BASE_URL, action]  parameters:params success:success failure:failure];

    return manager;
}
以上是response类中的相关方法。现在-每当请求抛出服务器错误通知时,无论它在应用程序中的何处,我都需要应用程序立即显示警报。因此,我认为只需在应用程序委托中放置一个处理程序:

 [[NSNotificationCenter defaultCenter] addObserverForName:@"Server Error" object:nil queue:nil usingBlock:^(NSNotification* notif){
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:[notif userInfo][@"error"] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
}];
问题是,当出现错误时,应用程序将冻结几分钟,然后显示警报。我知道这与应用程序生命周期有关。有没有一种方法可以实现我想要的(从代码简单性的角度来看),而不让应用程序冻结几分钟?我只是不知道如何解决这个问题


谢谢大家!

不可能是应用程序生命周期。。。请记住,通知是在后台线程中发出的,因此您可能应该在主线程上显示警报?下一个绘图周期可能会在将来某个时候发生,但您不知道…我如何在主线程上显示它?使用dispatch_async(dispatch_get_main_queue(),^{//show alertview});作品非常感谢。请写下你的答案,这样我才能给你正确的答案