Pergunta

Flex Mobile applications are view based. And I'm using Adobe Cirrus (im assuming its the same for any FMS netConnection) Does anyone know how to maintain a persistent netConnection between views in a flex Mobile Application?

edit: to try and explain what i need more clearly...

So real simple here I am connecting to cirrus

netConnection = new NetConnection();
netConnection.addEventListener(NetStatusEvent.NET_STATUS, cirrusStatusHandler);
netConnection.connect(CirrusAddress + "/" + DeveloperKey);

Then I have this to trigger certain functions depending on the status of the connection,

protected function cirrusStatusHandler(event:NetStatusEvent):void

                switch (event.info.code)
                {
                    case "NetConnection.Connect.Closed" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Rejected" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Failed" :
                        trace(event.info.code);
                        break;

                    case "NetConnection.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Posting.Notify" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Neighbor.Connect" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.Neighbor.Disconnect" :
                        trace(event.info.code);
                        break;

                    case "NetGroup.SendTo.Notify" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Connect.Success" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Publish.Start" :
                        trace(event.info.code);

                        break;

                    case "NetStream.Play.Start" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Play.Failed" :
                        trace(event.info.code);
                        break;


                    case "NetStream.Play.Stop" :
                        trace(event.info.code);
                        break;

                    case "NetStream.Connect.Closed" :
                        trace(event.info.code);
                        break;  

                    case "NetStream.Play.UnpublishNotify" :
                        trace(event.info.code);
                        break;  

                    case "NetStream.Unpublish.Success" :
                        trace(event.info.code);
                        break;

                }
            }

I want to be able to trace(netConnection.nearID) on this view, go to another view and trace(netConnection.nearID) and get the same result. and be able to have a cirrusStatusHandler() function like I have a above to listen for cirrus events. Then be able to do netConnection.close() on another view if i wanted to to be able to close the connection.

Brainstorming Ideas what I was thinking I could do:

I was thinking I could maybe set up the connection on the main ViewNavigatorApplication mxml file, but i have so much going on and being triggered based on netConnection status events it seems it might be really complicated to handle everything from that mxml file on each of the views.

Maybe I could declare the netCon vars in the ViewNavigatorApplication mxml file, and just add event listeners to those vars on each view?

But i'm not familiar with accessing vars that exist in the mainViewNavigatorApplication mxml file

I just need to be able to make the connection once, and then it stays persistent until I call netConnection.close()

Any ideas? Is this possible? Simple? Really complicated?

I guess I could use separate views where I dont need the netConnection and just have this particular view use "states" inside the view where the netCon needs to be persistent. It just seems silly be be using states since this is a view based application.

EDIT: @ Flextras Answer:

Updated:

Here's what i've gotten to compile without any errors (until i debug then i get a crash ill explain)

Here is the main ViewNavigatorApplication file MYAPP.mxml I put this in there:

public static var netConnection:NetConnection;
public static var groupspec:GroupSpecifier; 
public static var netGroup:NetGroup;

views.HomeView.netConnection = netConnection;
views.ProfileView.netConnection = netConnection;
views.HomeView.groupspec = groupspec;
views.ProfileView.groupspec = groupspec;
views.HomeView.netGroup = netGroup;
views.ProfileView.netGroup = netGroup;

then in my views package.. ProfileView.mxml:

public static var netConnection:NetConnection;
public static var groupspec:GroupSpecifier;
public static var netGroup:NetGroup;

and the same in my Home View

but i'm getting a null error when i try and call

trace(netConnection.nearID) in the crationComplete function on profileView (which is after homeView) to see if it still has the same netConnection and should then be able to get the same nearID

Foi útil?

Solução 4

THIS IS THE ANSWER: stackoverflow won't allow me to mark this an answer for 2 days. I'll mark it as such then sorry that its being left open.

Alright, I always knew you could pass a data object when pushing views, and i've been using it for basic information and things, but I didn't know you can do something like this with a netConnection, i just didn't think it would work like that. and performance wise I guess this is the way to go for mobile apps because instead of forcing something to run the whole time im just passing it on through the views will each previous view is being destroyed.

Here's all it took. Heres he vars I already had on my firstView:

public var netConnection:NetConnection;
public var groupspec:GroupSpecifier;
public var netGroup:NetGroup;

Then i just put those vars in an object, and when pushing a view just like you would pass any other data into a view:

    var goToNextView:Object=new Object();
    goToNextView.netConnection = netConnection;
    goToNextView.groupspec = groupspec;
    goToNextView.netGroup = netGroup;

    navigator.pushView(views.ProfileView,goToProfileObject);

then on the next view, got the data from the object like this:

netConnection = data.netConnection;
groupspec= data.groupspec;
netGroup = data.netGroup;

then I did trace(netConnection.nearID) on the new view, and it was the same as on the view before, so it works!

Thanks for getting me in the right direction for solving this flexcoders, The solution you posted was more about setting up global vars, this solution is using the flex mobile ablication navigator to pass objects the way they indented i guess.

Outras dicas

It sounds like you're mixing the responsibility of View & Services.

Take a look at some frameworks to help you with an MVC approach - I'd suggest Parsley (because it's what I know), but I'm hearing that RobotLegs is also great, and possibily more suited to a mobile application,

Generally speaking - views shouldn't have explicit knowledge of services - these are generally declared elsewhere, (like within a Context using Parsley, or RobotLegs.)

Logic for your views get encapsulated in a Presentation Model (PM). This is particularly important when building mobile apps, where the same logic is likely to apply to many different versions of your view (ie., Android, iOS, Tablet, etc).

Then you can use messaging (Events) for sending status updates, and use PM's for holding persistent state across your application.

If you meant to ask "How can I share the same NetConnection between multiple views".

Just give each view a reference to the same NetConnection object. Each view can have it's own, different listeners to the events dispatched from the NetConnection object.

It doesn't have to be any more complicated than creating a property in the ProfileView and HomeView classes, like this:

public var netConnectionInstance : NetConnection;

You can, quite literally, put the same line in both classes.

In the class that contains both views, do something like this:

var netConnectionInstance = new NetConnection()
profileView.netConnectionInstance  = netConnectionInstance;
homeView.netConnectionInstance  = netConnectionInstance;

I'm assuming in this case that both profileView and homeView are children of the same class. Things can get a bit more complicated if your have a more complicated display object structure; but the concept is the same.

You can use a design pattern like Singleton to maintain a persistent connection between your views

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top