开发者

UISwitch invokes a Restful API call which fails, would like to revert UISwitch value

My application is a VOIP telephony toolbox.

I have a series of UISwitch controls, which the user can use to change their settings, for example if they want to alter their caller id settings.

When the user changes the setting I need to make a call to the Telephony platform over its Restful API. If the Restful call fails, then I would like to reset the switch back to its previous setting. eg If the user turns caller ID on, and it fails because of a connection failure, I would like the switch to revert back to off.

I implemented this in my switchChangedValue method, however it creates a nasty loop. When a failure happens I set the UISwitch to its previous setting, but it in turn calls the switchChangedValue method again, which fails and so on looping

Here is part of my switchChangedValue method, any ideas welcome.

//Check if its a valid response from the XSI server
if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
    //This is the successful case       
}
else
{

    // I throw an alert here            

    //Id really like to change the UISwitch back if it goes wrong but it causes a bad loop.     
        if (buttonstate == false){开发者_JS百科
        [switchbutton setOn:YES animated:YES];
                    //This invokes my switchChangedValue again 

    }
    else if (buttonstate == true){
        [switchbutton setOn:NO animated:YES];
                    //This invokes my switchChangedValue again
    } else{
        NSLog(@"Something went bad");
    }


[bs release];


You might try something like this:

Declare this in your header:

BOOL _fireAPICall;

Set it to YES whenever the particular class you're in is initialized:

- (id)init {
    if (self = [super init]) {
        ...
        _fireAPICall = YES;
        ...
    }
    return self;
}

Then:

if (_fireAPICall) {
    if ([bs getHTTPResponseCode] >= 200 && [bs getHTTPResponseCode] < 300) {
        // success
    } else {
        // failure
        _fireAPICall = NO;
        [switchbutton setOn:!buttonstate animated:YES];
    }
} else {
    _fireAPICall = YES;
    // handle case where switch is turned off if necessary
}

This is assuming that you're not making an API call when the user manually turns the switch off, though - is that the case?

Updated above!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜