Activity Indicator while calling API

I want to display the activity indicator while waiting for the API to return. The problem is after all the result I get from API, then the spinner only display. The result I want is while waiting for API call, then the spinner will running.

I’m calling this method in here

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
   [self startLoadingSpinner]
   //Calling API...
   [self stopLoadingSpinner]
}

Here is the method for the activity indicator

-(void)startLoadingSpinner {
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    self.activityIndicator.opaque = YES;
    self.activityIndicator.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.4f];
    self.activityIndicator.center = self.view.center;
    self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.activityIndicator setColor:[UIColor whiteColor]];
    [self.view addSubview:self.activityIndicator];
    [self.activityIndicator startAnimating];
}

This is how I stop the activity indicator

-(void)stopLoadingSpinner {
    [self.activityIndicator performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.5];
}

Don’t add activity indicators in tableview datasource method – numberOfRowsInSection .

Add these two functions calling in the same method where you are making an API call. Make an API call in ViewDidLoad, some life cycle method or in action methods.

Below is the example of using it.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

[self startLoadingSpinner]

NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error) {
        NSLog(@"Error: %@", error);

    } else {
        NSLog(@"%@ %@", response, responseObject);
    }

    [self stopLoadingSpinner]
}];
[dataTask resume];

In Swift

func makeAPIRequest(to endPoint: String) {

    // here you can showActivetyIndicator start progressing here
    self.startLoadingSpinner()   
    Alamofire.request(endPoint).responseJSON{ response in
        if let value = response.result.value {
            let responseInJSON = JSON(value)
            self._responseInJSON = responseInJSON
        }
       // here you can hide Your ActivetyIndicator here
      self.stopLoadingSpinner()
    }
}

My detailed answer is below

-(void)simpleGetResponse{
    @try {
            //Call the Activity Indicator show method here
            [self startLoadingSpinner];
            NSString *strURL = @"Your URL";
            NSURL *urlStr = [NSURL URLWithString:strURL];
            NSMutableURLRequest *mutaURL = [NSMutableURLRequest requestWithURL:urlStr];
            [mutaURL setHTTPMethod:@"GET"];
            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:mutaURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                              {
                                                  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                                  if(httpResponse.statusCode == 200)
                                                  {
                                                      NSError *parseError = nil;
                                                      id response = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                                                      if(response != nil){
                                                          if([response isKindOfClass:[NSDictionary class]]){
                                                              NSLog(@"response is in dictionary format %@",response);
                                                              NSDictionary *dictRes = [response copy];
                                                              NSLog(@"The dictRes is - %@",dictRes);
                                                          }
                                                          else{
                                                              NSLog(@"response is in array format %@",response);
                                                              NSDictionary *arrRes = [response copy];
                                                              NSLog(@"The arrRes is - %@",arrRes);
                                                          }
                                                          dispatch_async(dispatch_get_main_queue(), ^{
                                                             //Call the Activity Indicator hidden method inside the dispatch_main_queue method
                                                             [self stopLoadingSpinner]
                                                             [yourTableView reloadData];
                                                          });
                                                      }
                                                  }
                                                  else
                                                  {
                                                      NSLog(@"Error");
                                                  }
                                              }];
            [dataTask resume];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", [exception description]);
    }
    @finally {
    }
}