PeterK
PeterK

Reputation: 4301

How to show UIActivityIndicationView when loading data

I am trying to show an indicator view when loading data in viewDidLoad but the loading starts before i display the indicator, i guess that is because the view is not loaded until after. I have been reading a bit about this but just cannot get it to work.

What i would want is to display the activity indicator during the loading of the files into the database.

I have been doing testing so the code structure may look a bit weird.

Could someone nice please give me a hint, or a link, how to fix this so the activity indicator is shown when/if data is loaded from the .txt files into the DB?

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @" ";

loadActivityIndicator.hidden = TRUE;  // Hide UIActivityIndicationView at start



[self loadDataIfNeeded];
}

-(void)loadDataIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];

myFileArray = [[NSMutableArray alloc]init];

//======SET THE FILE INPUT NAME======//
qFileTxtName = @"110615";
[myFileArray addObject:qFileTxtName];
//===================================//

// CHECK IF THE FILE EXISTS
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"checkQuestionFile.plist"];

if([fileManager fileExistsAtPath:path]) {
    NSArray *readKeyFileName = [[NSArray alloc] initWithContentsOfFile:path];

    if ([[readKeyFileName objectAtIndex:0] isEqualToString:qFileTxtName]) {
        //==== THERE IS NO UPDATED QUESTION .txt FILE ====//
    }
    else {
        //==== THERE IS AN UPDATED QUESTION .txt FILE ====//

        //  SET UP UIActivityIndicator view to show that work is ongoing
        loadActivityIndicator.hidden = FALSE;
        [loadActivityIndicator startAnimating];


        //==== SET UP PATH FOR ALL FILES====//

        NSString *aString = [[NSString alloc]init];

        aString = [@"questions_famquiz_hard_" stringByAppendingString:qFileTxtName];
        NSString *path1 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];

        aString = [@"questions_famquiz_medium_" stringByAppendingString:qFileTxtName];
        NSString *path2 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];

        aString = [@"questions_famquiz_easy_" stringByAppendingString:qFileTxtName];
        NSString *path3 = [[NSBundle mainBundle] pathForResource:aString ofType:@"txt"];


        AccessQuestionsDB *accessQuestionDataFunction = [AccessQuestionsDB new];
        idCounter = [accessQuestionDataFunction populateTheDatabase: path1 theID:0 firstTime: YES];
        idCounter = [accessQuestionDataFunction populateTheDatabase: path2 theID:idCounter firstTime: NO];
        idCounter = [accessQuestionDataFunction populateTheDatabase: path3 theID:idCounter firstTime: NO];


        //==UPDATE THE PLIST==//
        [myFileArray writeToFile:path atomically: TRUE];

        //  Stop UIActivityIndicator as activity is over
        loadActivityIndicator.hidden = TRUE;
        [loadActivityIndicator stopAnimating];


    }



} else {
    //== If file not found write a new file ==//
    [myFileArray addObject:qFileTxtName];
    [myFileArray writeToFile:path atomically: TRUE];
}
}

PROBLEM SOLVED

I replaced the call

[self loadDataIfNeeded];

with;

[NSThread detachNewThreadSelector:@selector(loadDataIfNeeded) toTarget:self withObject:nil];

to achieve multi threading according to the recommendation from Jonah :-)

Upvotes: 0

Views: 255

Answers (3)

Jonah
Jonah

Reputation: 17958

You are loading your data synchronously in the main thread. That means that the main thread, the one responsible for drawing the UI, is busy loading your data and will not have a chance to update your view until after your loadDataIfNeeded method finishes (at which point you don't want to show your activity indicator to be visible anymore anyway).

Display your activity indicator on the main thread but then allow the main thread's run loop to continue and instead perform expensive operations (like loading data or performing network requests) asynchronously on a secondary thread.

Look at NSThread, NSObject's -performSelectorInBackground:withObject:, and NSOperationQueue for different options for performing tasks off of the main thread.

Upvotes: 1

Alex Terente
Alex Terente

Reputation: 12036

In your viewDidLoad add

    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityView.frame = CGRectMake(0, 0, 44, 44);
    activityView.center = self.view.center;
    activityView.tag = 99;
    [activityView startAnimating];

    [self.view addSubview: activityView];

    [activityView release];

Then in your load method after you finish loading just remove your activity indicator from super view.

So in your loadDataIfNeeded after loading add

[[self.view viewWithTag:99] removeFromSuperview];

Upvotes: 0

werner
werner

Reputation: 859

The book "IOS Recipes" by Matt Drance has a recipe that talks about how to do this best. You can get this recipe as a free excerpt from the book at http://media.pragprog.com/titles/cdirec/activity.pdf

You can find the source code for the recipe on the book page at http://pragprog.com/titles/cdirec/ios-recipes

Upvotes: 0

Related Questions