Search code examples
iosobjective-cbolts-framework

Method within BFTASK getting delayed even with completion of task


I'm not sure why this is happening but when I run a query on AWS which uses BFTASK a method within it does not get complete until long after the BFTASK handler is complete. NSLog is printed in the console at the right time but the code to change the state of the UILabel isn't applied till long after.

For example, I have

    [[dynamoDBObjectMapper query:[UsernameDB class] expression:queryExpression] continueWithBlock:^id(BFTask *task) {

        if (task.error) {

            NSLog(@"Error: [%@]", task.error);

            });

        } else {

//Other code
            if(x  > 0){

                [self notAvailable];

            } else{
                [self available];

            }

        }

Which calls

-(void)available{

    NSLog(@"Username Available");
    self.availabilityLabel.text = @"Available";
    self.availabilityLabel.textColor = [UIColor greenColor];

}

Now what confuses me is that the NSLOG is printed in the console at the right time but the change in the UILabel is not applied till long after the method has been called.


Solution

  • Make sure that the labels are updated on the UI thread (main thread). Wrap them in a dispatch async call to the main thread.

    dispatch_async(dispatch_get_main_queue(), ^{
       self.availabilityLabel.text = @"Available";
       self.availabilityLabel.textColor = [UIColor greenColor];
    });