22

I've made a today widget for the german ice hockey league DEL.

I'm loading the next games from our server an show them in a tableView. The loading process is started in the proposed method "widgetPerformUpdateWithCompletionHandler". Initially i'm loading some cached data in "viewWillAppear".

Everything works great so far!

Screenshot of the working widget.

But after a while (one day) the widget stops working. When I open the notification center the widget appears normal, but it is never updated again. I have to remove the widget from the notification center and have to add it again. After that the widget works for a day and then again it stops working.

To see what the widget ist doing, I've added a simple white view with a status text above the table view while loading the data in "widgetPerformUpdateWithCompletionHandler" to see if the widget is doing anything. The white view appears when the widget is working. When it is not working the status view doesn't appear. So I think the method "widgetPerformUpdateWithCompletionHandler" isn't called after the widget is active in the notification center for a while.

I've got no clue what causes the widget to stop working. Any ideas?

Wilko X
  • 320
  • 2
  • 13

4 Answers4

19

I've got the same problem and I resolved it by calling completionHandler(NCUpdateResultNoData); right after your network request even when the response hasn't been returned. I found that if completion handler is not called the widgetPerformUpdateWithCompletionHandler will no longer get invoked, and therefore there won't be any more updates. Also make sure you call completion handler in all branches after your request call returns.

Si Yi Cathy Meng
  • 314
  • 4
  • 10
  • Yes, I found the same. I tried some things and figured out that when the network request takes some time (bad network conditions) widgetPerformUpdateWithCompletionHandler is never called again. I think the completion handler becomes invalid somehow. Your solution will work, but this is not the intended way. I solved the problem by not using widgetPerformUpdateWithCompletionHandler at all. I hope the issue is solved in the future. – Wilko X Oct 09 '14 at 16:54
  • So u are invoking your network request somewhere else if not using widgetPerformUpdateWithCompletionHandler? – Si Yi Cathy Meng Oct 10 '14 at 03:44
  • Yes I invoke the network reuqest in viewDidAppear. So the widget will not refresh in the background, but every time the user is opening it. – Wilko X Oct 10 '14 at 05:05
  • invoking update on viewDidAppear is not the right (at least not the Apple suggested) way to update your widget. The widget is refreshed every time you open it even using widgetPerformUpdateWithCompletionHandler. – valvoline Oct 16 '14 at 07:06
  • 3
    Same issue for me. I'm making a copy of the block in the `widgetPerformUpdateWithCompletionHandler` method : `self .completionHandler = _CompletionHandler;`. Then I call it when the async task completed. But after some time (several hours?) the widget `viewDidLoad` is called, but `widgetPerformUpdateWithCompletionHandler` isn't called anymore. So my widget appear empty with about 50px height. Is the block copy unsafe? Or should I start my async task in `viewDidLoad`? – Raphaël Pinto Oct 23 '14 at 07:54
  • Surely this is not how it is intended to be used? – bencallis Mar 22 '15 at 23:06
13

As others have mentioned, this is caused by not having previous called the completionHandler after widgetPerformUpdateWithCompletionHandler has been called. You need to make sure that completionHandler is called no matter what.

The way I suggest handling this is by saving the completionHandler as an instance variable, and then calling it with failed in viewDidDisappear:

@property(nonatomic, copy) void (^completionHandler)(NCUpdateResult);
@property(nonatomic) BOOL hasSignaled;

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
    self.completionHandler = completionHandler;
    // Do work.
}

- (void)viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];
  if (!self.hasSignaled) [self signalComplete:NCUpdateResultFailed];
}

- (void)signalComplete:(NCUpdateResult)updateResult {
  NSLog(@"Signaling complete: %lu", updateResult);
  self.hasSignaled = YES;
  if (self.completionHandler) self.completionHandler(updateResult);
}
BigCheesy
  • 1,128
  • 8
  • 14
  • I tried this to as It seemed like a sensible solution to ensure the completion block was always called. Sadly using this approach does not fully solve the issue. – bencallis Mar 16 '15 at 21:23
  • Wish I could give gold here, like on reddit. – maksa May 18 '15 at 16:46
  • Do you also need to reset hasSignaled to NO each time widgetPerformUpdateWithCompletionHandler is called? – RealCasually Jan 24 '16 at 18:22
3

An additional problem is that once widgetPerformUpdateWithCompletionHandler: is stopped being called, there will never be another completion handler to call. I haven't found a way to make the system call widgetPerformUpdateWithCompletionHandler: again. Therefore, make sure your widget will also try to reload data through viewWillAppear: as a fallback, or some users might be stuck with a non-loading widget.

Rachid Finge Jr
  • 1,171
  • 10
  • 14
  • 1
    Sadly this is what I ended up doing, its a shame that trying to follow the proper use of widgetPerformUpdateWithCompletionHandler does not work even in iOS 9. Thank you for this suggestion, its the only thing that actually solved this issue for me. – Polar Bear Sep 19 '15 at 02:47
  • How did you approach doing this? I presume without a proper completion handler, the system will never know the widget is back in action, and therefore will rely solely on the viewWillAppear calls? Do you do any synchronization to ensure when functioning properly that things aren't updated twice repeatedly? – RealCasually Jan 24 '16 at 18:18
1

Calling the completionHandler with NCUpdateResultNewData within widgetPerformUpdateWithCompletionHandler before an async call comes back and calls it again with NCUpdateResultNewData or NCUpdateResultFailed seems to work.

Ari Braginsky
  • 928
  • 1
  • 11
  • 21