Andrew Ho
Andrew Ho

Reputation: 638

UIswitch not getting updated after getting value

In the app I am making the app gets the value from a webservice to check if someone is following somebody else. When the value is YES the slider has to be updated to ON. When I update the switch in the ViewDidLoad the switch gets updated. In the code after the webservice check the switch isnt being switched on. The 'YES, user is following' does show up in the log file.

   if ([data length] >0 && error == nil){
        NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", html);

            if ([html isEqualToString:@"isfollowing"]) {
                NSLog(@"YES, user is following");
                SwitchOutlet.on = YES;

                    }
    }

Upvotes: 0

Views: 163

Answers (1)

Martin Koles
Martin Koles

Reputation: 5247

If you do the webservice check on the background thread, then you need to update UI on the main thread. Replace this line:

SwitchOutlet.on = YES;

with a block:

dispatch_async(dispatch_get_main_queue(), ^{

    SwitchOutlet.on = YES;
});

and let us know.

Upvotes: 1

Related Questions