vivek bhoraniya
vivek bhoraniya

Reputation: 1535

Creating loop of request

Here is my code for making simple post request:

    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 ;
}];

Now I have put debug point in AFURLConnectionManager.h at this method:

 - (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];
});

}

When I run my program it is going number of times in this loop I dont know why this is happening.From this request I am getting tabledata if my table data is 4 then it will goes in 4 times in this loop. If it is 3 then it will go 3 times. Now when I select any data from table and push any other view controller and when I simple return from that viewcontroller to tableview it is again going in this method though I am not making any request.

Upvotes: 1

Views: 138

Answers (1)

Rob
Rob

Reputation: 437432

If you're making only one request, but seeing AFNetworking operations start multiple times, you might want to examine some of the properties here (notably self.request) to see what's getting requested multiple times.

I wonder if you have other AFNetworking calls that might have slipped your attention. Perhaps you are you using AFNetworking's UIImageView category method, setImageWithURL, or something like that.

Upvotes: 1

Related Questions