Question

I'm trying to update a label while different tasks are proceeding. I searched and used different options and endup using this way but it still doesn't work:

[processStatusLable performSelectorOnMainThread:@selector(setText:) withObject:@"Creating your account..." waitUntilDone:NO];
DCConnector *dccon = [DCConnector new];
ContactsConnector *conCon = [ContactsConnector new];

if (![dccon existUsersData]) {
    [dccon saveUsersInformation:device :usDTO];
    //created account

    //get friends -> Server call
    [processStatusLable performSelectorOnMainThread:@selector(setText:) withObject:@"Checking for friends..." waitUntilDone:NO];
    NSMutableArray *array = [conCon getAllContactsOnPhone];
    // save friends
    [processStatusLable performSelectorOnMainThread:@selector(setText:) withObject:@"Saving friends.." waitUntilDone:NO];
    if ([dccon saveContacts:array]) {
        [processStatusLable performSelectorOnMainThread:@selector(setText:) withObject:@"Friends saved successfully.." waitUntilDone:NO];
    }
}

The last performSelector is getting executed (at least I see the label text changed on the view), but all other selectors are not working. Any idea why?

EDIT 1

- (void)updateLabelText:(NSString *)newText {
    processStatusLable.text = newText;
}
Was it helpful?

Solution

we can use the following code to run something on the main thread,

dispatch_async(dispatch_get_main_queue(), ^{
  //set text label
});

Using that we can write a method like this,

- (void)updateLabelText:(NSString *)newText {    
    dispatch_async(dispatch_get_main_queue(), ^{
       processStatusLable.text = newText;
    });   
}

Finally, you can use change your code this way,

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    [self updateLabelText:@"Creating your account..."];
    DCConnector *dccon = [DCConnector new];
    ContactsConnector *conCon = [ContactsConnector new];

    if (![dccon existUsersData]) {
        [dccon saveUsersInformation:device :usDTO];
        //created account

        //get friends -> Server call
        [self updateLabelText:@"Checking for friends..."];
        NSMutableArray *array = [conCon getAllContactsOnPhone];
        // save friends
        [self updateLabelText:@"Saving friends.."];
        if ([dccon saveContacts:array]) {
        [self updateLabelText:@"Friends saved successfully.."];
        }
    }
});

OTHER TIPS

How fast do you run through this sequence of updates? If it is faster than a second, you aren't likely going to see all of 'em.

Making them wait until done is unlikely to impact anything as the drawing is done asynchronously anyway.

Note that your method names are unconventional; methods shouldn't be prefixed with get and saveUsersInformation:: is discouraged (try something like saveUsersInformationToDevice:usingDTO:).


How much time elapses between the calls to update the text field? The whole process takes a minute, but how is that time divided?

What is your main event loop doing otherwise? Running modally or running normally?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top