views:

21

answers:

1

I have an ipad app that is loading data remotely into core data, I was to constantly display a status update to a label but it looks like everything in my method has to be completed before the message is sent to the UILabel.

How do I get around this?

Sample Code:

-(void) importCollections {
/* code left out for brevity */

    for (int j=0; j <[[myCollections objectAtIndex:i] count]; j++)
            {

                Collection  *entity = (Collection*) [NSEntityDescription insertNewObjectForEntityForName:@"Collection" inManagedObjectContext:managedObjectContext];
                [entity setCollectionName:[[[myCollections objectAtIndex:i] objectAtIndex:j] valueForKey:@"CollectionName"]];
                [entity setCollectionID:[[[myCollections objectAtIndex:i] objectAtIndex:j] valueForKey:@"CollectionID"]];
                [entity setManufacturer:[manufacturers objectAtIndex:i]];

                NSError *error;

                if (![managedObjectContext save:&error]) {
                    // Handle the error.
                    NSLog(@"%@",error);
                }
                importStatus.text =[NSString stringWithFormat:@"importing collection: %@", entity.CollectionName];
            }
}

In the code above the importStatus is the UILabel I need to update constantly, but it seems to wait until after everything in this method is completed.

+1  A: 

You probably call importCollections from the main thread. This way you don't give UIKit a chance to update the UI as long as you are blocking the main thread and don't return to the run loop.

You should do the lengthy computation or loading of resources on a background thread. Because you can only update UI elements from the main thread, you have to wrap your UIKit calls into performSelectorOnMainThread:withObject:waitUntilDone:.

Nikolai Ruhe
makes sense, so I fire off all of my importing on a background thread and that will be able to send messages to the UILabel on the main thread and keep the user informed - am I understanding that correctly?
Slee
@Max Fraser: Exactly. Everything that takes longer than a couple of milliseconds should be done on a background thread. Thread communication is easy in Cocoa, due to the run loop architecture and performSelectorOnThread... API's of NSObject.
Nikolai Ruhe