How to save state to the server in an AS3 Flex App before unloading the SWF? - actionscript-3

I would like to use URLLoader to save my user's state to the server when the SWF is shutting down. I have a function named saveUserState which saves the user's state correctly when called at any point except shutting down.
Within my creationComplete function, how do I add an event listener to know when the main SWF is unloading?
I have tried:
addEventListener(Event.REMOVED_FROM_STAGE, saveUserState, true, 1000, true);
But my saveUserState function is not being called when the SWF is closed. I've searched and found other answers on the Internet which require an external interface to JS which does the invocation on the browser's onbeforeunload, but I see issues with IE and I'd like to avoid requiring this JS. I also see this answer, but it's not marked as accepted and I found it not to work.
Is this possible?

Try to save user state to server when state changed. In this case you will be protected from unexpected events like disconnect or power or internet failure.

Related

ServiceLocationProvider is null when launched as a Share Target

I'm using MVVM Light and everything is fine except when launching my Windows Phone 8.1 WinRT app as a Share Target.
When I try to assign MainViewModel viewModel = ServiceLocator.Current.GetInstance<MainViewModel>(); I get an exception for ServiceLocator.Current.
Exception Message: ServiceLocationProvider must be set.
Do I need to do something extra in App.xaml.cs OnShareTargetActivated event to insure the Locator is running?
UPDATE:
A ShareTarget page needs to be thought of as a small extension of your app. It seems that not all of the app's resources are loaded (including app-wide resources in App.xaml). So I just created a new instance of MainViewModel in the share page's constructor, loaded only the things I need for the share to complete, save the information and call ShareOperation.ReportCompleted. This returns the user back to the app that is sharing.
I still haven't found a good solution for getting other resources in my ViewModel, but this works for now.
This indicates that the following line has not been executed:
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
This line will instruct the ServiceLocator class to use the SimpleIoc.Default instance as its ServiceLocator.Current. When you run your app as a Share target, the initialization is slightly different and probably the ViewModelLocator doesn't get initialized. You need to find a good location to perform the initialization before you use the ServiceLocator.
Cheers
Laurent

Prism with WinRT how can I force a fresh start when resuming from termination?

I am building a WinRT App, that uses proximity, and WiFi direct for peer to peer communication. As a result, when the app terminates, and then resumes I need it to start fresh (the connections will be closed, and can't be reopened without user interaction). The problem is that the Prism MvvmAppBase class that I am inheriting my app from is doing something that is causing it to try to resume from a saved state (that does not exist) and the app ends up on the last screen shown, but there is no ViewModel backing it, and so depending on the view, it will just sit unresponsive, or crash.
I am looking at this guide for guidance, and it says that unless there is a way to start fresh, but I cannot seem to find how to actually do that. http://msdn.microsoft.com/en-us/library/windows/apps/xx130647.aspx
I have been hacking around in the App.cs file to try and get it to work. There is really nothing at all in the App.cs file now except for the unity container and prism bootstrapping, and a call to NavigationService.Suspending() in the Suspending event handler.
The bootstrapping looks like this, but it is never called when the app is resumed from Termination.
protected override async void OnLaunchApplication(LaunchActivatedEventArgs args)
{
await BootStrapper.Config(_container);
await BootStrapper.RegisterPrismInstances(_container, NavigationService, SessionStateService, FlyoutService);
NavigationService.Navigate("Main", null);
}
If anyone has dealt with this before, and can point me in the right direction, I would really appreciate it.
When a Prism WinRT app is re-launched after being Terminated Prism will try to restore the application state, the Frame's navigation stack and the Frame's state before being terminated (which will navigate to the last opened page and try to restore any properties in the view model that are marked with the RestorableState attribute.)
By looking at the MvvmAppBase's source code it seems that there are a couple of things you could try to prevent Prism for saving / restoring the application state:
Create a default constructor in your App class that would clear the handlers of the Suspending event. The default constructor of the MvvmAppBase registers to this event and saves the state when it's raised.
Override the OnLaunched method of the base class. In it, after executing the base method, check if the previous executing state is Terminated. If so, you could clear the navigation history of the NavigationService and navigate to your start up page. The saving and restoring operations will still execute though, so any registered service will still be restored to its previous state. (This cannot be done in the OnLaunchApplication as it's not invoked if the application's state was successfully restored.)
Also, you could also try to completely remove this functionality from the MvvmAppBase class. However, most of its methods related to saving / restoring the application state are private, so you might as well drop the MvvAppBase, copy its entire code in your App class and edit it accordingly.
I have not tried any of the approaches listed above so I'm unaware if they could generate any problem, but they might help you as a starting point.

LocalConnection works when it should fail

in my page i have a main swf, which should check the existence of a second swf - if its missing, the main swf should display a certain message to the user, otherwise it should work as planned.
i thought to achieve this by using LocalConnection - the main swf is set as the sending swf, and it should call a certain function on the other swf. i was thinking that if the second swf is missing, the local connection would fail - either throw an error or its STATUS event listener would receive an 'error' in the event's level property.
if you think as i did - well think again. no error was thrown and the listener got a 'status' in the event's level property, although there's nobody to handle the LocalConnection 'send' method.
what did i miss? or is there a better way to achieve my goal?
cheers,
eRez

How do I attach a global event listener?

I am working on an AIR application:
The main window is like a dashboard. With the menu bar, I can open other windows with dashboard details. When I close these, I'd like to refresh the main window.
I tried to use an event listener, but the result is not good. If I open detail windows directly from the main window, I know how to add an event listener - and it works - but I don't know how to do it, if the detail window is opening from the menubar!
Thanks for helping me.
A Singleton is what you are looking for. Just put an event dispatcher inside and you will be able to listen from everywhere in the application.
A Singleton is like having a unique instance of an object in memory, so anyone modifying a variable inside that object ( or sending events throught ) will be modified for everyone.
Here is an example of code on how to use it.
http://life.neophi.com/danielr/2006/10/singleton_pattern_in_as3.html
Note: Singletons are powerful and dangerous at the same time, there is a lot of talk about how to use them, please read a little more about that if you are considering building a big project.
Hope it helps!
The issue is that you're performing business logic from a View. Don't do this. Instead, dispatch an event from each menu rather than directly opening the window from within it. Listen for those events at a higher level, and then you can either directly listen to the new windows you have opened, or you can create a base window Class that exposes a variable of type IEventDispatcher. If you populate that variable with the same event dispatcher, what you wind up with is called an "event bus," and you can listen on that for events.
This architecture requires a little more thought than using a Singleton, but it avoids the tight coupling and other issues you'll run into by introducing one into your project.
You can listen to an object (EventDispatcher) directly by adding an event listener to it, or if the dispatcher object is on the displaylist, such as a Sprite, you could listen at the stage level with the capture parameter set to true.
But the main caveat is that the dispatcher must be on stage for you to catch this event.
Your main window listens to stage (with capture = true):
stage.addEventListener("MY_CUSTOM_EVENT", handle_custom_event, true);
private function handle_custom_event(e:Event):void
{
var sub_window:Object = e.target;
// do something to your sub_window
}
Your sub window can dispatch events like this:
dispatchEvent(new Event("MY_CUSTOM_EVENT"));
But (ab)using the stage as a message passing infrastructure for custom events in this way is a little messy. You could consider a more formal message passing architecture if you really want this kind of communication. Even a static MessageBus class would at least quickly help you identify where you use this in your codebase. Either way, you'll have to be careful about references and memory leaks.

event listener for flex/air action script errors

I'm trying to add an event listener to my air application that would prevent the "ActionScript error" window from appearing, so I can handle the error within my application.
I was able to find a little information about this from adobe. I'm just not sure what I should be listening for.
It depends quite a lot on what error is thrown, and why.
Your best bet is to carefully read the ActionScript documentation and add listeners to react to all of the errors that have explicit ErrorEvents (such as IOErrorEvent and SecurityErrorEvent). Those are usually related to network and/or file access, and security issues.
For most other errors, there is the try {} catch() {} finally {} statements. This tutorial might be a good place to start.
And if all else fails, there's UncaughtErrorEvent.
But you should really be using that one as a last resort, not as a magic bullet - the best error handling is a) trying to prevent errors from being thrown in the first place (make sure all variables are properly initialized, test for null, etc.), and b) handling expected runtime errors by catching them explicitly, in order to keep the application running and stable.
You have a couple options. As you know, exception handling is not always possible for certain asynchronous operations.
First off, you need to know what object is responsible for the asynchronous operation that is causing the error. The most sensible approach would be to add the necessary error event handlers to this object.
For instance, a URLLoader performs asynchronous operations; and it's failure can only be handled by adding error event listeners. For example:
var loader: URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, completeHandler);
loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
Another 'catch-all' option is to take advanage of the new UncaughtErrorEvent feature of Flash Player 10.1. For this to work, you need to attach the uncaught error handler to the loader of the main application; this will catch everything! For example:
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, loaderErrorHandler);
private function loaderErrorHandler(e:UncaughtErrorEvent):void {
if(event.error is Error) {
// handle error from embedded SWF
}
// suppress error dialog
e.preventDefault();
}
The last option may not be the best approach as it promotes the swallowing of exceptions instead of addressing and handling the problem properly; nevertheless it can be useful in certain unique circumstances (embedding SWFs).
The window won't appear if you're running the standard version of Flash Player.
It will manifest only as a dialog box on the debugger versions of the
browser plug-ins and stand-alone players, as a message in the output
panel in the authoring player, and as an entry in the log file for
Adobe Flex Builder 3. It will not manifest at all in the release
versions of Flash Player or AIR.
Source : here.