创建请求循环

时间:2014-06-17 05:08:15

标签: ios ios7 afnetworking afnetworking-2

以下是我发布简单帖子请求的代码:

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer=[AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:Token forHTTPHeaderField:Authorization];
NSLog(@" Headers%@",manager.requestSerializer.HTTPRequestHeaders);
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
[manager POST:URLAdvanceSearch parameters:searchDictionary success:^(AFHTTPRequestOperation *operation, id root) {
    NSLog(@"JSON: %@", root[@"data"]);
    if([[root valueForKey:@"isError"] boolValue]==0){
        NSLog(@"searchresult %@",root);
        //MyAppDelegate.searchResultArray=[root valueForKey:@"searchResult"];
        MyAppDelegate.searchResultArray=[[NSMutableArray alloc]init];
        [self compute:[root valueForKey:@"searchResult"] completionBlock:^(BOOL result){
            if(result){

                SearchResultVC.PinArray=[[NSMutableArray alloc]initWithArray:pinArray];
                [self.navigationController pushViewController:SearchResultVC animated:YES];
            }
        }];

    }
    else{
        [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window type:AJNotificationTypeRed
                                       title:[root objectForKey:@"message"]
                             linedBackground:AJLinedBackgroundTypeDisabled
                                   hideAfter:GZAJNotificationDelay];
        return ;
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
    NSLog(@"%@",error.localizedFailureReason);
    if (operation.response.statusCode==401) {
        [MyAppDelegate LoginRequired:Nil];
        [MyAppDelegate ClearLoginData];
    }
    [AJNotificationView showNoticeInView:[[UIApplication sharedApplication] delegate].window
                                    type:AJNotificationTypeRed
                                   title:error.localizedDescription
                         linedBackground:AJLinedBackgroundTypeDisabled
                               hideAfter:GZAJNotificationDelay];
    return ;
}];

现在我已经在此方法中将调试点放在AFURLConnectionManager.h中:

 - (void)operationDidStart {
[self.lock lock];
if (![self isCancelled]) {
    self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];

    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    for (NSString *runLoopMode in self.runLoopModes) {
        [self.connection scheduleInRunLoop:runLoop forMode:runLoopMode];
        [self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode];
    }

    [self.connection start];
}
[self.lock unlock];

dispatch_async(dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self];
});

}

当我运行我的程序时,它会在这个循环中多次出现我不知道为什么会发生这种情况。从这个请求我得到tabledata如果我的表数据是4那么它将在这个循环中进行4次。如果它是3那么它会去3次。现在,当我从表中选择任何数据并推送任何其他视图控制器时,当我从viewcontroller简单地返回到tableview时,它再次进入此方法,但我没有提出任何请求。

1 个答案:

答案 0 :(得分:1)

如果您只发出一个请求,但看到AFNetworking操作多次启动,您可能需要检查一些属性(特别是self.request)以查看多次请求的内容。

我想知道你是否有其他可能引起你注意的AFNetworking电话。也许您正在使用AFNetworking的UIImageView类别方法,setImageWithURL或类似的方法。