问题
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!
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?
回答1:
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.
回答2:
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);
}
回答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.
回答4:
Calling the completionHandler with NCUpdateResultNewData within widgetPerformUpdateWithCompletionHandler before an async call comes back and calls it again with NCUpdateResultNewData or NCUpdateResultFailed seems to work.
来源:https://stackoverflow.com/questions/25961513/ios-8-today-widget-stops-working-after-a-while