Search code examples
iosobjective-cafnetworking-2

svprogresshud not showing on main thread


On viewdidload of my tableviewcontroller i have the following

[SVProgressHUD showWithStatus:@"Loading"];
[self getData];
[SVProgressHUD dismiss];

On the getData method, i am using AFNetworking to get the data from my backend api. Since that is an asynchronous call, i would expect my SVProgressHUD to show.

  -(void) getData {
     AFHTTPRequestOperationManager * reqManager =AFHTTPRequestOperationManager manager];
     AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    //set auth token..etc 
   [reqManager GET:urlString parameters:nil 
    success:^(AFHTTPRequestOperation *operation, id responseObject) {
             //code for success....
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       //code for failure
    }];
}

But it's not showing at all. So obviously i am working on my main thread. Where am i going wrong ?


Solution

  • The problem is you are showing hud immediately after method call like

    [self getData];
    [SVProgressHUD dismiss];
    

    This is the problem. Move dismiss code to

    -(void) getData {
         AFHTTPRequestOperationManager * reqManager =AFHTTPRequestOperationManager manager];
         AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
        //set auth token..etc 
       [reqManager GET:urlString parameters:nil 
        success:^(AFHTTPRequestOperation *operation, id responseObject) {
                 //code for success....
                 [SVProgressHUD dismiss];
        }
        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           //code for failure
                  [SVProgressHUD dismiss];
        }];
    }
    

    You can see the dismiss call in success and failure of webservice call, and call the getData method by

    [SVProgressHUD showWithStatus:@"Loading"];
    [self getData];
    

    remove the [SVProgressHUD dismiss] from here.