1

i want to show data asynchronously, it work successfuly and i get response quickly in my log message but it takes too much time to hide spinner, and also i am unable to show data into my tableview quickly, can i know what is reason for it?

-(void) clubsDetail:(int)catID
{

    NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenu];
    NSLog(@"%@",link);

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:link]];
    [request setRequestMethod:@"POST"];

    [request setPostValue:[NSString stringWithFormat:@"%d",  catID] forKey:@"cat_id"];
    NSLog(@"%d",catID);
    [request setCompletionBlock:^{
        NSString *jsonString = [request responseString];
        NSLog(@"response %@",jsonString);
        NSArray *dataArray = (NSArray *) [jsonString JSONValue];
        NSDictionary * returnDicto = nil;
        NSLog(@"response after parsin");
//        NSDictionary * returnDict = [returnDicto objectForKey:@"club_info"] ;

        [slideDetail_dataArray removeAllObjects];
//        if ([returnDict objectForKey:@"club_info"]) {


        for(returnDicto in dataArray)
        {
            PostClubDC *postDC = [[PostClubDC alloc] init];
            NSDictionary * returnDict = [returnDicto objectForKey:@"club_info"] ;
            postDC.postID = [[returnDict objectForKey:@"Id"]integerValue];
            postDC.postCatID = [[returnDict objectForKey:@"cat_id"]integerValue];
            postDC.postName = [returnDict objectForKey:@"name"];
            postDC.postHeadLine = [returnDict objectForKey:@"headline"];
            postDC.postImage = [returnDict objectForKey:@"image"];
            postDC.postLat = [returnDict objectForKey:@"long"];
            postDC.postAddress = [returnDict objectForKey:@"address"];
            postDC.postSummary = [returnDict objectForKey:@"summary"];
            postDC.postStat = [returnDict objectForKey:@"stat"];
            postDC.postUS = [returnDict objectForKey:@"us"];
            NSDictionary * returnDictic = [returnDicto objectForKey:@"club_rating"];
            postDC.postGoodRate = [returnDictic objectForKey:@"good"];
            postDC.postGreatRate = [returnDictic objectForKey:@"great"];
            [slideDetail_dataArray addObject:postDC];
        }

        // hide spinner
        [[myActivityIndicator currentIndicator]hide];
        [tbl_slideDetail reloadData];
    }];
//    [request setFailedBlock:^{
//        NSError *error = [request error];
//    }];
    [request startAsynchronous];
    // show spinner

    [[myActivityIndicator currentIndicator] displayActivity:@"Loading"];
}

2 Answers2

1

I think your problem is that you are trying to change the UI from a thread other than the main. Try changing this code to see if this helps:

    // hide spinner
    dispatch_async(dispatch_get_main_queue(), ^{
        // hide spinner on main thread
        [[myActivityIndicator currentIndicator]hide];
        [tbl_slideDetail reloadData];
    });
gWiz
  • 1,274
  • 9
  • 12
  • Well, when you make an async request the response executes on a separate thread (completionBlock). You cannot change anything UI related from a thread other than the main. So what we do is tell the app to run this piece of code on the main thread. This is done by using `dispatch_async(dispatch_get_main_queue(), ^{ ... });`. – gWiz Mar 05 '14 at 10:53
0

Since you asked what you should do in the comments, switch to AFNetworking 2.0. AFN uses blocks instead of delegate methods, and once you get used to how it works, it is much easier to work with than ASI.

Here is a link to a (very) old SO question that has an example of how to work with AFN (this has changed slightly for version 2.0). Lots of updated examples online (Ray Wenderlich)

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62