Show Activity Indicator when fetching JSON data

I am beginner in IOS programming. My question is my app fetching data from JSON in my web server, when starting the apps, it is slightly lag and delay due to the fetching process, so i would like to show activity indicator when i connecting to JSON data. How can i do that?

My JSON coding:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURL *urlAddress = [NSURL URLWithString:@"http://emercallsys.webege.com/RedBoxApp/getEvents.php"]; 
    NSStringEncoding *encoding = NULL;
    NSError *error;

    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:&error];
    NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
    if (dict)
    {
        eventsDetail = [[dict objectForKey:@"eventsDetail"] retain];
    }

    [jsonreturn release];
}

Answers


use the following code

//add a UIActivityIndicatorView to your nib file and add an outlet to it
[indicator startAnimating];
indicator.hidesWhenStopped = YES;

dispatch_queue_t queue = dispatch_get_global_queue(
                                                   DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    //Load the json on another thread
    NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:urlAddress usedEncoding:encoding error:NULL];
    [jsonreturn release];        
    //When json is loaded stop the indicator
    [indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];
});

You can use something like below code:

- (void)fetchData
{
    [activityIndicator startAnimating];

    NSURL *url = [NSURL URLWithString:strUrl];

    NSURLRequest *theRequest = [[NSURLRequest alloc]initWithURL:url];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if(theConnection) {

    }
    else {
        NSLog(@"The Connection is NULL");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    webData = [[NSMutableData data] retain];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [webData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction %@",error);
    UIAlertView *connectionAlert = [[UIAlertView alloc] initWithTitle:@"Information !" message:@"Internet / Service Connection Error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [connectionAlert show];
    [connectionAlert release];

    [connection release];
    [webData release];

    return;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    //NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    //NSLog(@"Received data :%@",theXML);
    //[theXML release];

    NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary: webData error:&error];

    if (dict) {
        eventsDetail = [[dict objectForKey:@"eventsDetail"] retain];
    }

    [activityIndicator stopAnimating];
}

Need Your Help

How can I terminate a system command with alarm in Perl?

perl system alarm

I am running the below code snippet on Windows. The server starts listening continuously after reading from client. I want to terminate this command after a time period.

using objects in different forms

c# object

I need help to be able to use an object from one form to the other one. Here is the code I have: