iOS 8 today widget stops working after a while - widget

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?

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.

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);
}

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.

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

Related

Page constructor called multiple times - Windows Phone 8

I have a page in a WP8 application, that every time I navigate to it, the constructor is called.
From what I know, the constructor of a page called only once at the first time the page loaded. my page is very heavy, and every construction takes wasted time..
this my navigation code, usual one:
NavigationService.Navigate(new Uri("/Views/Pages/ContentControlNew.xaml", UriKind.Relative));
and this is the constructor of the page:
public ContentControlNew()
{
InitializeComponent();
}
Not special.. is it normal that the constructor is called every time? Please tell me if you need more details because I don't know what else to say about this subject.
Yes this is normal because whenever you use NavigationService.Navigate it always creates a new page object and adds that (pushes it) to the navigation stack. For example when you use GoBack() it pops it out of the stack and destroys it, but when it gets back to the previous page it doesn't call the constructor since that one was already in the stack and does not have to be recreated.
If you don't want to create a page every time you navigate to it, you should look into Navigation Models for Windows Phone for some ideas on how you can tackle this.

Swing JTabbedPane : addChangeListener or addContainerListener or both?

I have one swing code written by other person. For swing tabbed pane, he has added both change and container listener and both calls the same method:
addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent theEvent ) {
someMethod();
}
} );
addContainerListener(new ContainerAdapter() {
public void componentAdded(ContainerEvent theEvent) {
someMethod();
}
public void componentRemoved(ContainerEvent theEvent) {
someMethod();
}
} );
Whenever tab is removed from this tabbed pane, it internally calls JTabbedPane.removeTabAt(int index), which in turn calls fireStateChanged() causing new change event listened by change listener.
Now as new component (tab) is removed from tabbed pane, it also calls componentRemoved(ContainerEvent theEvent) method of container listener.
Both change even and container events, then calls same method someMethod(), which does set background and foreground colors.
I would like to know, if this kind code might cause some issues. Recently we are facing random IndexOutOfBoundException exeptions. I am just wondering, if this is causing this issue.
Also as per my understanding in swing, once event is listened, logic inside it should be executed using worker thread (e.g. SwingWorker). Please let me know if this is correct.
I am new to swing, thus any hint would be appreciated.
Thanks.
Whenever tab is removed from this tabbed pane, it internally calls
JTabbedPane.removeTabAt(int index), which in turn calls
fireStateChanged() causing new change event listened by change
listener.
This is true if the removed tab is also the selected tab. In the other cases, you won't be notified.
You need to choose what event you want to listen to:
Addition/Removal of components?--> go for ContainerListener
Selected tab? --> go for ChangeListener
I would like to know, if this kind code might cause some issues.
Recently we are facing random IndexOutOfBoundException exeptions. I am
just wondering, if this is causing this issue.
Since there is no line in your sample code that could throw that Exception, it is impossible to answer your question. Post an SSCCE that shows your issue.
Also as per my understanding in swing, once event is listened, logic
inside it should be executed using worker thread (e.g. SwingWorker).
Please let me know if this is correct.
It depends:
If you need to modify anything in the UI, anything related to Swing, it needs to be executed on the EDT (Event Dispatching Thread) and thus, SwingWorker is not an option.
If you need to perform business logic operations, and especially if they can be lengthy, then you should indeed use a SwingWorker or any other mechanism to execute that code in another thread than the EDT. Consider visiting the Swing tag wiki on "Concurrency"

Is there a way in ActionScript to force an IMMEDIATE update of the stage, _not_ after an event?

I know the method MouseEvent.updateAfterEvent() or KeyboardEvent.updateAfterEvent() which will force a re-render of the stage just after the event is handled rather than waiting for the next frame.
However, I need a method to force an immediate render at the very moment I call it. Is there such a method?
Actually my problem comes from the demential design of ActionScript's printing API (PrintJob). Inconsistent with the whole ActionScript architecture, when you call PrintJob.start(), everything is completely frozen while the printing dialog is shown until the user clicks the print or cancel button. Execution of any code after the PrintJob.start() call is resumed after that.
Among a lot of other much worse issues coming from this gigantic design flaw, there is mine:
public function someMouseOrKeyboardEventHandler() {
somethingThatUpdatesTheDisplayList();
var somePrintJob=new PrintJob();
somePrintJob.start();
//...
somePrintJob.send();
}
When this handler of mine is called, the changes made to the display list will not be visible until after the printing dialog has been closed, so I can't, for example, show something on the screen just before I open the print dialog.
updateAfterEvent() won't help a bit (already tried it). It won't change a thing, since it only forces rendering after the event handler code is executed.
Is there any updateRightNow()-like thing?
Nope, you unfortunately can't force an update in the middle of your code.
You can, however, wait until the next frame to call start() on the PrintJob; this will give Flash time to update the stage before everything freezes.

Facebook Login Handler doesn't get fired in Flex4 Facebook API

I have literally spent HOURS trying to solve this mystery... but simply can't seem to get hold of it.
I am using the same code lines (literally!) as the example here (official adobe tutorial) and I get different result.
protected function login():void
{
Facebook.login(loginHandler,{perms:"user_birthday,read_stream,publish_stream"});
}
protected function loginHandler(success:Object,fail:Object):void
{
trace ("login handler called");
if(success){
currentState="state_home";
Facebook.api("/me",getMeHandler);
//userImg.source=Facebook.getImageUrl(success.uid,"small");
Facebook.api("/me/statuses",getStatusHandler);
}
}
Everything works fine, i.e. everything till it is time to fire the loggedin event. I get asked to log in and all permissions are asked correctly. After I log myself in to facebook, the loggedin event doesn't fire. Is there any way of solving this problem??
And I am really desparate... :(
Have you tried debugging the JavaScript of the containing HTML page? I've found that the JavaScript can encounter errors in the communication back to your Flash movie, and it fails silently in the background.
Open the debugger and have a look for any errors - sometimes its down to conflicting embed object Ids in the HTML that throws it.

MooTools - Re-use Request Object

I would like to re-use a Request.JSON object, but I am not sure how. I'm looking for something like the following example:
// In initialize/constructor
this.request = new Request.JSON( {
method : 'get'
});
// Elsewhere
this.request.setOptions({
url : 'http://...',
onSuccess : onSuccess,
onFailure : onFailure
}).send();
there are going to be certain issues with this kind of approach.
if you only have the one instance handling all requests, then you need to make sure whilst a request is taking place, there is nothing else that can restart it with the new options as its asynchronous. additionally, events will stack up. every new instance that you run will addEvent onComplete/onSuccess/onFailure and they won't always be relevant. so you need to apply removeEvents() to the request instance before each run.
have a look here http://www.jsfiddle.net/dimitar/8a7LG/4/
i am not showing this as an example of how i'd write it but to see the problems that come with it. click the second link first then the first one (jsfiddle adds 2 seconds network lag) and you will see the alert from the second link's onComplete event stacked up on the first one as well. further more, for each click on link 2 you will see a new alert in addition to the old ones.
you must also consider how applicable it is to extend Request.JSON instead but it all depends on your needs.
p.s. if you go over to Request.JSONP this kind of structure may play some tricks, in particular with the callback functions being reset etc.
best of luck :)
edit here's the thing working with removeEvents so you don't get the stacking up: http://www.jsfiddle.net/dimitar/8a7LG/5/
I don't differ with the accepted answer's point, but it actually sidesteps the question IMHO.
Take a look at the following blog post (it's not mine); there, under the subtitle Multiple Links with Request.HTML you can find some guidance about how to reuse the Request instance.
http://ryanflorence.com/basic-ajax-and-json-requests-with-mootools/