简体   繁体   中英

View is not populating data AFNetworking

I am using AFNetworking, my success block works fine, but my view is always loaded first. I am not using table view for my view, there are only labels, and images that need to be refreshed. What should i do now ?

-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
       NSLog(@"%@",IDs);
        NSDictionary *params = @{@"Id" : IDs};
         NSLog(@"%@",link);
        [manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
        NSMutableArray *dataArray = [[NSMutableArray alloc] init];

        ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
        clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
        clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
        clubDC.cDName = [responseObject objectForKey:@"name"];
        clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
        clubDC.cDImage = [responseObject objectForKey:@"image"];
        clubDC.cDLong = [responseObject objectForKey:@"long"];
        clubDC.cDLat = [responseObject objectForKey:@"lat"];
        clubDC.cDAddress = [responseObject objectForKey:@"address"];
        clubDC.cDSummary = [responseObject objectForKey:@"summary"];
        clubDC.cDStat = [responseObject objectForKey:@"stat"];
        clubDC.cDUS = [responseObject objectForKey:@"us"];
        clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];

        [dataArray addObject:clubDC];
        success(dataArray);
        if (dataArray.count == 0)
        {
            ALERT_VIEW(@"Please check your internet connection.");
        }

    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
             ALERT_VIEW(@"Error while loading data.")
         }];
}

and my venuVC.m is like this where i want to show data

-(void)reloadView
{
    clubDetail = [[ClubDetailDC alloc] init];
    venu_dataArray = [[NSMutableArray alloc] init];
    upcomingArray = [[NSMutableArray alloc] init];
    GET_DBHANDLER
    //    venu_dataArray = [dbHandler get_club_detail:venuID];
    NSString *strVenuID = [NSString stringWithFormat:@"%d",venuID];
    NSLog(@"%@",strVenuID);
    [dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
     {
         venu_dataArray = array;
         //         NSLog(@"%d",array.count);
     }];

}
-(void)viewWillAppear:(BOOL)animated
{
    [self reloadView];

    for (clubDetail in venu_dataArray)
    {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
    }
    UIFont* font_name = [UIFont fontWithName:@"Corbel" size:17];
    UIFont* bold_font = [UIFont fontWithName:@"Corbel-Bold" size:17];
    lbl_upcomingEvents.font = font_name;
    lbl_Unighted.font = bold_font;
    lblDescription.font = [UIFont fontWithName:@"Corbel" size:11];
    lblCheckIn.font = [UIFont fontWithName:@"Corbel" size:13];
    lblCount.font = [UIFont fontWithName:@"Corbel" size:12];
    lblGoodEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
    lblHotEmotion.font = [UIFont fontWithName:@"Corbel" size:14];
}

My problem is that view is loaded first, then I get the data from success block. Can anyone help?

Thanks.

Since venue_dataArray is local in reloadView method, you wont be able to iterate it in viewWillAppear . So I guess, you should move your label assigning code to success block like below:

[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
 {
     venu_dataArray = array;
     //         NSLog(@"%d",array.count);
     for (clubDetail in venu_dataArray)
     {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
     }
 }];

you can do this in two ways either you write function in in your view, code will be like this

-(void)myVenusWithClubDetail :(NSString *)IDs completionHandler:(void (^)(id array))success
{

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    NSString *link = [NSString stringWithFormat:@"%@",KWSURLVenuDetail];
    NSLog(@"%@",IDs);
    NSDictionary *params = @{@"Id" : IDs};
    NSLog(@"%@",link);

    [manager POST:link parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"JSON: %@", responseObject);


        NSMutableArray *dataArray = [[NSMutableArray alloc] init];

        ClubDetailDC *clubDC = [[ClubDetailDC alloc] init];
        clubDC.cDVenuID = [[responseObject objectForKey:@"Id"]integerValue];
        clubDC.cDCatID = [[responseObject objectForKey:@"cat_id"]integerValue];
        clubDC.cDName = [responseObject objectForKey:@"name"];
        clubDC.cDHeadLine = [responseObject objectForKey:@"headline"];
        clubDC.cDImage = [responseObject objectForKey:@"image"];
        clubDC.cDLong = [responseObject objectForKey:@"long"];
        clubDC.cDLat = [responseObject objectForKey:@"lat"];
        clubDC.cDAddress = [responseObject objectForKey:@"address"];
        clubDC.cDSummary = [responseObject objectForKey:@"summary"];
        clubDC.cDStat = [responseObject objectForKey:@"stat"];
        clubDC.cDUS = [responseObject objectForKey:@"us"];
        clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];
        for (clubDetail in venu_dataArray)
        {
            lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
            [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
        }



        [dataArray addObject:clubDC];
        success(dataArray);

        if (dataArray.count == 0)
        {
            ALERT_VIEW(@"Please check your internet connection.");
        }

    }
          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              NSLog(@"Error: %@", error);
              ALERT_VIEW(@"Error while loading data.")

          }];


}

or if you want to use your function in separete class then do like this in your success block

[dbHandler myVenusWithClubDetail:strVenuID completionHandler:^(id array)
 {
     venu_dataArray = array;
     //         NSLog(@"%d",array.count);
     for (clubDetail in venu_dataArray)
     {
        lblDescription.text = [ NSString stringWithFormat:@"Neighborhood:%@ Information on whats happening tonight", clubDetail.cDAddress];
        [imgClub setImageWithURL:[NSURL URLWithString:clubDetail.cDImage]];
     }
 }];

Can you post your success function too? If you set the text on your labels and image on your uiimageview everything should be updated accordingly.

Your success function could look something like:

- (void)updateViewsWithData:(NSMutableArray *)dataArray {
    var clubDC = dataArray[0];
    self.yourLabel.text = clubDC.cDSummary;
    self.addressLabel.text = clubDC.cDAddress;
    self.imageView.image = clubDC.cDImage;
}

You have to AFNetworking library or something similar to download the image first because I am pretty sure that what you got is the url of the image.

Please comment if you have any more question.

In your code,

clubDC.cDImage = [self loadClubDetailImages:clubDC.cDImage];

This line will fetch the image from server or local directory depending upon how you implemented it. There will be expected delay in getting the image.

self.imageView.image = clubDC.cDImage;

there will be chance that image is not fully downloaded when you are executing above code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM