PhilipsHue / PhilipsHueSDK-iOS-OSX

The Software Development Kit for Philips Hue on iOS and OS X (beta)
579 stars 169 forks source link

updateScheduleWithSchedule often returns "The operation couldn't be completed." error #83

Open jfhamel opened 9 years ago

jfhamel commented 9 years ago

Hi,

I'm currently developing an application that triggers a schedule update with a UISwitch. Unfortunately, I often get the following error: "Error Domain=com.philips.hue.sdk Code=92 \"The operation couldn\U2019t be completed. (com.philips.hue.sdk error 92.)\""

I realized that I systematically get the error when I tap the switch approximately just before the heartbeat occurs. I feel that there are interferences between the heartbeat and the calls the the PHBridgeSendAPI method calls.

In the first thing, I would expect that the SDK manages all this. It should queue all the bridge activities one after the other. Is it a bug or I miss something?

I also tried many things to get around the issue:

  1. I tried to stop the heartbeat with [UIAppDelegate.phHueSDK disableLocalConnection] before calling updateScheduleWithSchedule and restart it with [UIAppDelegate.phHueSDK enableLocalConnection] in the completion handler of [BridgeSendAPI updateScheduleWithSchedule... but I got the same result.
  2. I tried with [UIAppDelegate.phHueSDK disableCacheUpdateLocalHeartbeat:YES] and ...NO with the same result.
  3. I tried to add some delay with [NSThread sleepForTimeInterval:1.0f] and [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]] after the call to [UIAppDelegate.phHueSDK disableLocalConnection] and before the call to [UIAppDelegate.phHueSDK enableLocalConnection] with the same result. I tried many length of delay.
  4. I also tried to move things around like calling [UIAppDelegate.phHueSDK disableLocalConnection] before initializing PHBridgeSendAPI *myBridgeSendAPI but without any effect.

The only way I found to get rid of the error is to stop the heartbeat. Unfortunately, I need it.

Can anybody help me with this?

My method code is below.

Thank you.

-(void) mySwitchOnOffScheduleWithSchedule:(PHSchedule *)mySchedule newStatus:(bool)myNewStatus
{
    PHBridgeSendAPI *myBridgeSendAPI = [[PHBridgeSendAPI alloc] init];

    //Stop heartbeat
    [UIAppDelegate.phHueSDK disableLocalConnection];

    PHSchedule *myNewSchedule=[mySchedule copy];
    [myNewSchedule setStatusAsEnum:myNewStatus ? SCHEDULE_STATUS_ENABLED : SCHEDULE_STATUS_DISABLED];

    [myBridgeSendAPI updateScheduleWithSchedule:myNewSchedule completionHandler:^(NSArray *myErrors)
     {
         if (myErrors!=nil)
         {
             NSString *myMessage=[NSString stringWithFormat:@"Errors: %@", myErrors];
             UIAlertView *myErrorAlert =[[UIAlertView alloc] initWithTitle:@"Response" message:myMessage delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
             [myErrorAlert show];
             NSLog(@"%@",myMessage);
         }

         //Restart heartbeat after schedule update completeion
         [NSThread sleepForTimeInterval:1.0f];
         [UIAppDelegate.phHueSDK enableLocalConnection];
         [UIAppDelegate.phHueSDK setLocalHeartbeatInterval:3.0f forResourceType:RESOURCES_ALL];
     }];
}