0

In the viewDidLoad method of my viewController I wrote something like this:

- (void)viewDidLoad {
[super viewDidLoad];

self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.hud.labelText = @"Loading";
[serverData loadNewsData:self ];
 }

Basically I show the HUD and start a method loadNewsData on another class, passing the viewController.

The method in the other class is like this:

 -(void)loadNewsData:(LoadViewController*) loadViewController
  {

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[siteAddress stringByAppendingString:getNewsList] parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {


    loadViewController.hud.detailsLabelText = @"updating news";

    [NewsModel setNewsData:responseObject];
     loadViewController.hud.detailsLabelText = @"Finished";

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

}

When the request of the AFHTTPRequestOperation starts I was expecting to see the sub menu in the HUD to change in "Updating news". But nothing happens. I just see Finished at the end of the process. The requests takes like 10sec so I was expecting to see "updating news", but nothing. I try also to this:

 dispatch_async(dispatch_get_main_queue(), ^{
       loadViewController.hud.detailsLabelText = @"updating news"; });

Thinking I have to be in the UI queue to update some UI stuff, but it did not work either. I'm a newbie probably I miss something.

thanks for any good advices.

NiBE
  • 857
  • 2
  • 16
  • 39
  • You wouldn't have a problem if you just changing "Loading" to "Updating News" right ? Kinda looks redundant to have "Loading". – Zhang Dec 10 '14 at 09:29
  • I wouldn't pass the `viewController` to the `serverData` helper object. I think it is better if you have a method in your VC that calls `loadNewsData` and has a completion block, where you can update the UI. And as the comment above, what's the difference between "Loading" and "Updating New" ? – koen Dec 11 '14 at 21:05

2 Answers2

1

Try to move loadViewController.hud.detailsLabelText = @"updating news"; before you start GET request. If you keep this inside the success block you won't see the text.

Fabio Felici
  • 2,841
  • 15
  • 21
  • Hi, I'd really like to be able to change it inside the block. – NiBE Dec 10 '14 at 08:42
  • But you can't because the success block is executed when the network call is finished... so `@"updating news"` is instantly replaced with `@"Finished"` – Fabio Felici Dec 10 '14 at 08:44
  • @GonjiDev is right. Once the success block is run, ALL the data has already been loaded... An alternative is to use a progress block if AFNetworking has one... Or your going to have to look into NSURLConnection. – jsetting32 Dec 10 '14 at 09:05
  • So I just checked the AFNetworking docs and apparently you can progressively get data back using `- (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;` Check the docs and you'll get a better understanding... – jsetting32 Dec 10 '14 at 09:08
0

This link should help you out... NSURLConnection

It describes the NSURLConnection delegate method to use and how to use it for the exact situation your in. I understand that this isn't an AFNetworking solution but if your still looking for an AFNetworking solution, check out this method - (void)setDownloadProgressBlock:(void (^)(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead))block;.

Hope this gets you in the right track.

Community
  • 1
  • 1
jsetting32
  • 1,632
  • 2
  • 20
  • 45