Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

SpiceManager in Application #211

Closed
MikolajKakol opened this issue Nov 4, 2013 · 8 comments
Closed

SpiceManager in Application #211

MikolajKakol opened this issue Nov 4, 2013 · 8 comments
Assignees
Labels

Comments

@MikolajKakol
Copy link

Hi,

I was wondering if it is possible to have common SpiceManager across all Activities, that would be accessible from Application class?

Answering my question would be: "yes, you can do that". But then how the SpiceService would be stopped? Any solutions/advises for that?

Best
Mikołaj

@rey5137
Copy link

rey5137 commented Nov 6, 2013

I usually using one MainActivity with many Fragment, so you can create a SpiceManager in MainActivity and share it with all Fragment.

@stephanenicolas
Copy link
Owner

You can also use inheritance to make a spice manager accessible in all your
activities. If you don't use it, it's quite light weight.

The problem with an application based spicemanager is that application
don't have an end on Android. Or, more precisely, you can't get notified of
their end. So, as you said, there is no real way to know the context is
going to die and stop the spice service. You won't be able to stop the
spice service explicitly.

Nevertheless, If the application gets killed, it will also kill its binding
and you can get notified of that. if the binding context dies, it should
stop the service due to some safe guard implemented in RS. So, that should
work, check your logs and see if the service is destroyed properly.

Also, I must add that what is really beneath your question is "how to
access something common in all activities ? ". For this, you should have a
look at Dependency Injection Frameworks on Android : namely either
RoboGuice or Dagger. Dagger is much faster but RoboGuice is much easier to
use. Anyhow, it looks like it's a good time for you to have a look at these.

Stéphane

2013/11/6 rey5137 notifications@github.com

I usually using one MainActivity with many Fragment, so you can create a
SpiceManager in MainActivity and share it with all Fragment.


Reply to this email directly or view it on GitHubhttps://github.com//issues/211#issuecomment-27840923
.

Stéphane NICOLAS,
OCTO Technology
Développeur & Consultant Android / Java
..........................................................
50, Avenue des Champs-Elysées
75008 Paris
+33 (0)6.26.32.34.09
www.octo.com - mobilite.octo.com
blog.octo.com - www.usievents.com
...........................................................

@mcastets
Copy link

mcastets commented Nov 6, 2013

@stephanenicolas For performance sake, is it ok to start and stop a SpiceManager in almost every fragments? Or would you recommend to follow @rey5137's way? Thanks

@stephanenicolas
Copy link
Owner

If you start the same service twice, only one instance will be started. I
don't think there is much performance difference in either approaches.

2013/11/6 Mathieu Castets notifications@github.com

@stephanenicolas https://github.com/stephanenicolas For performance
sake, is it ok to start and stop a SpiceManager in almost every fragments?
Or would you recommend to go @rey5137 https://github.com/rey5137's way?
Thanks


Reply to this email directly or view it on GitHubhttps://github.com//issues/211#issuecomment-27856300
.

Stéphane NICOLAS,
OCTO Technology
Développeur & Consultant Android / Java
..........................................................
50, Avenue des Champs-Elysées
75008 Paris
+33 (0)6.26.32.34.09
www.octo.com - mobilite.octo.com
blog.octo.com - www.usievents.com
...........................................................

@mcastets
Copy link

mcastets commented Nov 6, 2013

So, if I understand, using inheritance, I'd better add the static keyword to declare the spiceManager? static final SpiceManager spiceManager = new SpiceManager(SpiceService.class)

@stephanenicolas
Copy link
Owner

I don't use static keyword generally speaking. It makes code hard to test
and breaks OO design.

No, just provide each activity a spiceManager and manage the life cycle in
the super class. That's it.

2013/11/6 Mathieu Castets notifications@github.com

So, if I understand, using inheritance, I'd better add the static keyword
to declare the spiceManager? static final SpiceManager spiceManager = new
SpiceManager(SpiceService.class)


Reply to this email directly or view it on GitHubhttps://github.com//issues/211#issuecomment-27861055
.

Stéphane NICOLAS,
OCTO Technology
Développeur & Consultant Android / Java
..........................................................
50, Avenue des Champs-Elysées
75008 Paris
+33 (0)6.26.32.34.09
www.octo.com - mobilite.octo.com
blog.octo.com - www.usievents.com
...........................................................

@MikolajKakol
Copy link
Author

I don't want to build application around any library. In my application architecture I've got few layers and I need the top layer that would be responsible for threading and networking and database management, so my Fragment/Activity doesn't know what is providing the data.

I didn't want to write it on my own since there are good libs like this. Basically what I have is Fragment that calls requestSomeData(), and then few layers later there is class that will receive that call, that has no idea about life cycle, and I want just to run it in separate thread and pass the result in UI thread to layer that would decide if it should update Fragment content.

So maybe it would be good just to automatically close SpiceManager if there was no new task for example 60seconds? Closing it every time when request queue is empty might be not that efficient.

Is RoboSpice good choice when I need to do some work in background thread check if there is netwrok if yes make network request and put result in db, else get data just from db, and return it to main thread?

@stephanenicolas
Copy link
Owner

@novemberox , yes RS is definitely a good choice for the use case you mention in the end. Although I must warn you that the ormlite module is a bit difficult to handle and as it remained orphan for a long time now, you will be more on your own than with any other module.

I definitely recommend you to use RS from within an context component like an Activity or a Fragment (or a service) more than the application. I don't think it's really a good idea to want to de-couple things as much as you want to. I mean, yes it would be great, but life cycle is really a big part of Android and you can't really circumvent it. We spent a lot of time tackling that whole issue in a correct way in RS and that's why the lib is so useful on Android. But I would be happy to hear about working alternatives.

I close the thread now as I think it was all a "talk/chat/discussion" rather than an issue per say. And after a few weeks off, I need to clean the backlog/issue board.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants