Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to display a loading indicator while getting data from a URL

I'm somewhat new to objective-c, i'm developing a news iOS application, the app gets all its contents using JSON parsing from a url, i'm using AFNetworking for that and this is the method that i made:

- (void)getContents
{
    NSString *urlString = @"http://some-url-that-has-json-output/";
    urlString = [urlString stringByAppendingString:self.articleId];
    NSLog(@"The call url is: %@",urlString);
    NSURL *url = [NSURL URLWithString:urlString];

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //AFNetworking asynchronous url request
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"The JSON data is: %@", responseObject);
        jsonContents = [responseObject objectForKey:@"article"];
        [self LoadStructure];
    } failure:nil];
    [operation start];
}

Now the data loads fine with this method.

My Question: How to display a loading indicator (could be a GIF) while getting the data ? and is this method above is the proper or best way to get data from a url ?

like image 293
ANA Avatar asked Dec 19 '25 14:12

ANA


2 Answers

You can use default loading indicator of iOS(UIActivityIndicator). You should start animating it before completion block and should hide inside success and failure block.

You should create a method using indicator as class variable:

indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.hidesWhenStopped = YES;
indicator.frame = CGRectMake(35, 15, 30, 30);
[self.view addSubview:indicator];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                     initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[indicator startAnimating];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"The JSON data is: %@", responseObject);

// to stop:

[indicator stopAnimating];
    jsonContents = [responseObject objectForKey:@"article"];
    [self LoadStructure];
} failure::^(AFHTTPRequestOperation *operation, NSError *error){
    [indicator stopAnimating];
}];
[operation start];
like image 194
Ritu Avatar answered Dec 22 '25 03:12

Ritu


Drag and drop UIActivityIndicatorView into your XIB view and connect it with IBOutlet.

.h file

@property(nonatomic, strong)IBOutlet UIActivityIndicatorView * activityIndicator;

Add UIActivity indicator view into your view. show it before

NSString *urlString = @"http://some-url-that-has-json-output/";

using:[self.activityIndicator startAnimating];

and stop it inside completion block and failure block

using: [self.activityIndicator stopAnimating];

like image 24
Bista Avatar answered Dec 22 '25 04:12

Bista



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!