title: Scheduling complication updates
body: <p>I have a custom complication on Apple Watch that I am trying to get to update once an hour. Each hour it should ping an API endpoint and if the data has changed from the last check, the complication should be updated.</p>
  
  <p>Here is what I currently have that only seems to work once in a blue moon. When it DOES work, it does indeed ping my server and update the complication. It appears WatchOS just isn't calling my scheduled tasked once per hour. Is there a better standard practice that I'm missing?</p>
  
  <pre><code>@implementation ExtensionDelegate
  
  - (void)applicationDidFinishLaunching {
      // Perform any final initialization of your application.
      [SessionManager sharedManager];
  
      [self scheduleHourlyUpdate];
  }
  
  - (void) scheduleHourlyUpdate {
      NSDate *nextHour = [[NSDate date] dateByAddingTimeInterval:(60 * 60)];
      NSDateComponents *dateComponents = [[NSCalendar currentCalendar]
      components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:nextHour];
  
      [[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate:nextHour userInfo:nil scheduledCompletion:^(NSError * _Nullable error) {
          // schedule another one in the next hour
          if (error != nil)
              NSLog(@"Error while scheduling background refresh task: %@", error.localizedDescription);
      }];
  }
  
  - (void)handleBackgroundTasks:(NSSet&lt;WKRefreshBackgroundTask *&gt; *)backgroundTasks {
      // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
      for (WKRefreshBackgroundTask * task in backgroundTasks) {
          // Check the Class of each task to decide how to process it
          if ([task isKindOfClass:[WKApplicationRefreshBackgroundTask class]]) {
              // Be sure to complete the background task once you’re done.
              WKApplicationRefreshBackgroundTask *backgroundTask = (WKApplicationRefreshBackgroundTask*)task;
              [backgroundTask setTaskCompletedWithSnapshot:NO];
              [self updateComplicationServer];
          } else if ([task isKindOfClass:[WKSnapshotRefreshBackgroundTask class]]) {
              // Snapshot tasks have a unique completion call, make sure to set your expiration date
              WKSnapshotRefreshBackgroundTask *snapshotTask = (WKSnapshotRefreshBackgroundTask*)task;
              [snapshotTask setTaskCompletedWithDefaultStateRestored:YES estimatedSnapshotExpiration:[NSDate distantFuture] userInfo:nil];
          } else if ([task isKindOfClass:[WKWatchConnectivityRefreshBackgroundTask class]]) {
              // Be sure to complete the background task once you’re done.
              WKWatchConnectivityRefreshBackgroundTask *backgroundTask = (WKWatchConnectivityRefreshBackgroundTask*)task;
              [backgroundTask setTaskCompletedWithSnapshot:NO];
          } else if ([task isKindOfClass:[WKURLSessionRefreshBackgroundTask class]]) {
              // Be sure to complete the background task once you’re done.
              WKURLSessionRefreshBackgroundTask *backgroundTask = (WKURLSessionRefreshBackgroundTask*)task;
              [backgroundTask setTaskCompletedWithSnapshot:NO];
          } else if ([task isKindOfClass:[WKRelevantShortcutRefreshBackgroundTask class]]) {
              // Be sure to complete the relevant-shortcut task once you’re done.
              WKRelevantShortcutRefreshBackgroundTask *relevantShortcutTask = (WKRelevantShortcutRefreshBackgroundTask*)task;
              [relevantShortcutTask setTaskCompletedWithSnapshot:NO];
          } else if ([task isKindOfClass:[WKIntentDidRunRefreshBackgroundTask class]]) {
              // Be sure to complete the intent-did-run task once you’re done.
              WKIntentDidRunRefreshBackgroundTask *intentDidRunTask = (WKIntentDidRunRefreshBackgroundTask*)task;
              [intentDidRunTask setTaskCompletedWithSnapshot:NO];
          } else {
              // make sure to complete unhandled task types
              [task setTaskCompletedWithSnapshot:NO];
          }
      }
  }
  
  - (void)updateComplicationServer {    
      [self scheduleHourlyUpdate];
  
      NSString *nsLogin = [NSUserDefaults.standardUserDefaults objectForKey:@"loginDTO"];
  
      if (nsLogin != nil)
      {
          NSDateComponents *dateComponents = [[NSCalendar currentCalendar]
          components: NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
  
          LoginDTO *login = new LoginDTO([nsLogin cStringUsingEncoding:NSUTF8StringEncoding]);
  
          NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.myurl.com/Api/Watch/Complication"]];
          [req setHTTPMethod:@"GET"];
  
          // Set headers
          [req addValue:[NSString stringWithUTF8String:login-&gt;GetApiKey()] forHTTPHeaderField:@"MySessionKey"];
          [req addValue:[NSString stringWithFormat:@"%d,%d,%d", dateComponents.year, dateComponents.month, dateComponents.day] forHTTPHeaderField:@"FetchDate"];
  
          [req addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  
          NSURLSession *session = [NSURLSession sharedSession];
          NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
          {
              // Call is complete and data has been received
              NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
              if (httpResponse.statusCode == 200)
              {
                  NSString* nsJson = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  
                  NSString *prevJson = [NSUserDefaults.standardUserDefaults objectForKey:@"previousComplicationJson"];
  
                  if (prevComplicationJson != nil)
                  {
                      if ([prevComplicationJson isEqualToString:nsJson])
                          return; // Nothing changed, so don't update the UI.
                  }
  
                  // Update the dictionary
                  [NSUserDefaults.standardUserDefaults setObject:nsJson forKey:@"previousComplicationJson"];
  
                  CLKComplicationServer *server = [CLKComplicationServer sharedInstance];
                  for (int i = 0; i &lt; server.activeComplications.count; i++)
                      [server reloadTimelineForComplication:server.activeComplications[i]];
              }
          }];
  
          [task resume];
  
          delete login;
      }
  }
  </code></pre>