delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

Wich is better way to manage connection? #83

Closed cristianhoyos66-zz closed 8 years ago

cristianhoyos66-zz commented 8 years ago

Hi, I do this question with the finality to know a better way to manage the server connection

I'm doing my project connection as I would do a connection in projects made with java language and mysql as db, so we guess that I have two collections: users and companies, each one have an associated activity, and in each activity I do this:

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       // ...

       // create a new instance
       mMeteor = new Meteor(this, "ws://example.meteor.com/websocket");

       // register the callback that will handle events and receive messages
       mMeteor.addCallback(this);

       // establish the connection
       mMeteor.connect();
   }

 @Override
   public void onDestroy() {
       mMeteor.disconnect();
       mMeteor.removeCallback(this);
       // or
       // mMeteor.removeCallbacks();

       // ...

       super.onDestroy();
   }

We can see that the connection is opened and closed respectively.

I think that I can use singleton to manage the connections with my server, but I don't know if it can be a good idea, because singleton is hard to test and I don't know if MeteorSingleton is thread safe.

I'd appreciate your help in this.

Thank you.

ocram commented 8 years ago

Thanks for this interesting question!

My recommendation would be that you should really use the singleton access pattern.

The singleton does not cause any problems that you wouldn't have with the standard access pattern (that you're currently using): Is the class itself (as opposed to the singleton) much easier to test? Not really. Does it provide memory-safety guarantees that the singleton does not provide? No.

What you certainly _can_t do is creating instances multiple times and connecting multiple times, as you have seen yourself :)

cristianhoyos66-zz commented 8 years ago

I'd really like to read your reasons by wich you recommend me use singleton and to know if you can tell me if meteor manage connection pool for the server connection, I know that mongo does, but I don't know meteor for the server.

Thank you so much.

ocram commented 8 years ago

I'm not really sure what you are referring to, sorry!

The singleton is the recommended access pattern for your case since it re-uses a single connection across all Activity instances. If you use the normal access pattern of creating instances via new Meteor(...), you will have a separate connection for every single Activity.

With that said, I'm not sure what your question regarding the connection pool is about or if that question has been answered already.

cristianhoyos66-zz commented 8 years ago

If i use singleton pattern could happen that the connection port on the server is occupated by an application in a device, and does not allow that another app in another device can connect to that server, because that port is not available?

ocram commented 8 years ago

Actually, the singleton pattern does only affect the one device and application that it's running on. There are no side effects to other devices.

Does that solve your problem?

cristianhoyos66-zz commented 8 years ago

Yes, you are so helpful, thank you so much.