Crystal
Crystal

Reputation: 29468

Debugging UI updates on the main thread, iPhone, Activity Indicator not spinning

so I know you are supposed to do your updates on the main thread. I am using Apple's code for writing our database do the documents directory:

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {
    // First, test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success)
        return;
    // The writable database does not exist, so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"bookdb.sql"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
    if (!success) {
        NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
    }
}

I do

[activityIndicator startAnimating];
[self createEditableCopyOfDatabaseIfNeeded];

I do not see my activity indicator. If I comment out [self createEditableCopyOfDatabaseIfNeeded];, I do see my activity indicator. Is there a way to debug this to see what is happening and why I don't see my spinning indicator? Thanks.

Upvotes: 0

Views: 310

Answers (1)

Krumelur
Krumelur

Reputation: 32507

Out of context, it is hard to say, but I assume that you want the activity indicator to represent the createEditableCopyOfDatabaseIfNeeded operation ongoing. Assuming that the [self createEditableCopyOfDatabaseIfNeeded] was called from your main thread, you can do something like this:

[activityIndicator startAnimating];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    [self createEditableCopyOfDatabaseIfNeeded];
    dispatch_async(dispatch_get_main_queue(), ^{
        [activityIndicator stopAnimating];
    });
});

Upvotes: 2

Related Questions