1

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

brybam
  • 5,009
  • 12
  • 51
  • 93
  • -1 from me. You seem to be trying to join a lot of different, isolated, independent concepts and turning this question into one big mass of confusion. You don't need to create a persistent connection between two items (AKA Views) that exist in the same memory space (AKA The Flash Player). They are, in essence, always connected. And there are plenty of approaches for sharing data between two different views. Do you need help with that? Both answers at the time of this writing try to help with that. Keeping a Flex client persistently connected to a server is a completely different issue. – JeffryHouser Jun 11 '11 at 22:26
  • um...Would it be more constructive for me to just say "How do i do this???" and that's it. nothing more. Rather than potential ideas to get some feedback on? Guess I get a -1 for brainstorming. Thanks! – brybam Jun 11 '11 at 22:56
  • @brybam I do not understand what you're asking. One of your questions is "Is this possible?" I don't know what the "this" is. Ditto for the question in your comment. "How do I do this?" How do you do what? Are you trying to share data between multiple views? Or are you trying to open a persistent connection to the server? They are both two very different questions and do not share the same solution--or even the same problem elements. Yet you switch between both topics as if they were identical. Please clarify your question. – JeffryHouser Jun 11 '11 at 23:06
  • Well, the title of my question is "How to maintain a persistent netConnection between Flex views?" So, one would assume "this" would means "How to maintain a persistent netConnection between Flex views?" I updated my question with a specific example of something I'd like to achieve. Maybe that would help you understand if you're still interested in looking at the topic. If not. Thanks anyway! – brybam Jun 11 '11 at 23:14
  • Maybe you don't understand what a NetConnection is. To quote docs: "The NetConnection class creates a bidirectional connection between a Flash Player or AIR application and a Flash Media Server application" http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/NetConnection.html . A netConnection is a client and server connection It has nothing to do with sharing data between two views. That said, i think your re-write is more clear. I believe you may have meant to ask "How can I share the same NetConnection Object between two views?" – JeffryHouser Jun 11 '11 at 23:27
  • I updated the title of the question, I think that sounds closer to what i'm trying to ask. I just wasn't sure how to ask it. Would it be as simple as referencing the netConnection var from View1 while in View2 and adding the event listener to it again in the new view? or attaching an object containing the netConnection var to it when pushing the view like `var dataObject:Object dataObject.netConnection = netConnection; navigator.pushview(views.View2, dataObject);` or am i really far off base again? – brybam Jun 11 '11 at 23:34
  • @brybam Check out my answer. I think you're on the right track. Yes, just send the instance of NetConnection to each view. I also removed my downvote; with the rewritten question and modified title. – JeffryHouser Jun 11 '11 at 23:53

4 Answers4

2

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.

dplante
  • 2,445
  • 3
  • 21
  • 27
Marty Pitt
  • 28,822
  • 36
  • 122
  • 195
1

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.

JeffryHouser
  • 39,401
  • 4
  • 38
  • 59
  • Thanks! I feel like this is almost it, It makes the most sense for what i need to do. I made some tests but got some errors, would you mind taking a look at what i added on the bottom of my question in response to this? – brybam Jun 11 '11 at 23:56
  • My second code snippet should be in the "Component" that contains both your ProfileView and HomeView. My answer was mistyped a bit; sorry about that. I'll fix it. – JeffryHouser Jun 12 '11 at 00:27
  • Yep that's exactly what I tried and it says `"Multiple markers at this line: -1119: Access of possibly undefined property netConnection through a reference with static type Class. -1120: Access of undefined property netConnection. -Access of undefined property netConnection -ProfileView` Are you familiar with how Flex mobile projects are setup? I could take a screen shot, im sure im doing it just like your suggesting – brybam Jun 12 '11 at 00:40
  • You're going to have to show more code than you have for me to distinguish what is wrong. The error in your errors suggests you may be referencing the class name "ProfileView" instead of the instance name "profileViewInstance." It's not a static propery; so you have to access it on the instance of the class; not the Class itself. – JeffryHouser Jun 12 '11 at 00:42
  • in flex mobile projects the views are in a views package, for example when you navigate to them you do like navigator.pushview(views.profileView) I noticed your code works if i make the var in the views a public static var. would this cause any issues with it being a static var in each view? – brybam Jun 12 '11 at 00:45
  • I only dont get any compile errors if i do what i did above, but it still doesnt work. i updated in my question above again if you wouldnt mind taking a look. thanks for helping me with this! Even though its been a pain – brybam Jun 12 '11 at 01:18
  • I'm pretty sure that, for performance reasons, a mobile project will destroy any view not currently displayed on the screen; and then recreate it when you navigate back to it. As such, you're probably going to have to pass in the NetConnection object to the relevant view each time the view changes. Off the top of my head, I don't know which events you would listen to to do that. – JeffryHouser Jun 12 '11 at 02:25
0

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

dizda
  • 79
  • 1
  • 7
  • I would really urge against this. Singleton is generally considered an antipattern these days, instead using Dependency Injection. Singletons promote heavily coupled code, lead to code that cannot be tested, and introduces state in places that shouldn't maintain state. – Marty Pitt Jun 11 '11 at 22:26
  • @Marty Singleton's don't actually have to be an anti-pattern, if they're done correctly (Static access to a centralized interface instead of a centralized class). – cwallenpoole Jun 11 '11 at 23:22
  • @Marty Pitt Most frameworks make heavy use of Singletons. There are tradeoffs w/ any architecture decision. The last RobotLegs app I built made heavy use of Singletons to share data between views (or more specifically, Mediators, which passed that data into views). I only mention this b/c RobotLegs was one aspect of your answer. – JeffryHouser Jun 11 '11 at 23:26
  • Respectfully, I disagree. Singletons inherently hide dependencies, (and therefore design flaws) and tightly couple your code to the singleton class. These guys articulate my thoughts much more cleverly than I can: http://bit.ly/RdDqi http://bit.ly/bTb3gX http://bit.ly/atNZCT – Marty Pitt Jun 12 '11 at 00:15
0

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.

brybam
  • 5,009
  • 12
  • 51
  • 93