Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ContextSingleton retention policy to RUNTIME #196

Closed
zawadz88 opened this issue Jan 31, 2017 · 9 comments
Closed

Change ContextSingleton retention policy to RUNTIME #196

zawadz88 opened this issue Jan 31, 2017 · 9 comments
Assignees
Milestone

Comments

@zawadz88
Copy link
Contributor

Hi,
First of all thanks for the great library!
Recently, I tried updating toothpick from 1.0.3 to 1.0.5 in my Android project and got:
Error:Scope Annotation toothpick.smoothie.annotations.ContextSingleton does not have RUNTIME retention policy. during compilation.
I checked and ContextSingleton from smoothie has runtime policy CLASS currently unlike e.g. Singleton from javax.inject or the custom scope in smoothie-sample which probably causes this error. This is probably due to the changes in #191

Could you help please?

@dlemures dlemures added this to the 1.0.6 milestone Jan 31, 2017
@dlemures
Copy link
Collaborator

Hi @zawadz88 ,

Thanks for the issue.

This class was meant to be there just as an example. However, the javadoc is not really clear and probably the class is not useful either.

Therefore, we will deprecate the class, change the retention and update the javadoc.
The class will be removed in a later release.

If you are using context singletons, please create the annotation yourself and bind it to the right scopes:
https://github.com/stephanenicolas/toothpick/wiki/Scope-Annotations#binding-a-scope-to-a-scope-annotation

@stephanenicolas
Copy link
Owner

stephanenicolas commented Feb 1, 2017

I would add that ContextSingleton was really a RoboGuice thing. It looked really cool at that time as you could quickly inject a context. At Groupon, we used a TP ContextSingleton for our migration from RG to TP.

But, later, we just removed it. The issue is that when you @Inject Context, then you just don't know exactly which context you are injecting : an activity or the app context ?? And one can create a leak, not the other one, simply depending on where you inject your entity.

So, definitely ContextSingleton is more a simple example of a custom annotation but it should not be used in a production app. At Groupon, we use instead Singleton, ActivityScope, ServiceScope, and we never inject a context, always either the Application or an Activity or a Service, so that we have full control over what is injected. And we use the scope verification mechanism of TP to be sure we don't leak an activity or a service.

@zawadz88 zawadz88 closed this as completed Feb 1, 2017
@zawadz88 zawadz88 reopened this Feb 1, 2017
@zawadz88
Copy link
Contributor Author

zawadz88 commented Feb 1, 2017

Thanks for the clarification and sorry for the misclick ;)

@dlemures dlemures self-assigned this Feb 1, 2017
@stephanenicolas stephanenicolas modified the milestones: 1.0.6, 1.0.7 Mar 25, 2017
@HugoGresse
Copy link

HugoGresse commented Jun 9, 2017

I was searching how to inject the Application in a @Singleton is there any example on the documentation/demo?
I also don't find any documentation about the SmoothieApplicationmodule. I'm just a beginner with DI :/

@stephanenicolas
Copy link
Owner

If you install the smoothie application module in your app scope, then you can simply use @Inject Application app in this scope or any sub scope.

All the docs of TP is in the wiki in the repo... @HugoGresse

@dlemures dlemures modified the milestones: 1.0.8, 1.0.7 Jun 20, 2017
@dlemures
Copy link
Collaborator

@HugoGresse were you able to do it?

@HugoGresse
Copy link

yep, that was fine. I've still some issue where the @Inject is done after constructor call which conduce me to delay some action. For example here. I've probably made something wrong but I am inject the scope on the class AFTER is instantation that maybe be this.

@stephanenicolas
Copy link
Owner

@HugoGresse there are 2 things you can do with TP, and I believe it's the same with dagger / Roboguice:

  • use a constructor with @Inject annotation, then all arguments will be created by TP (it needs to have a provider / or a binding or the classes to be annotated properly).
class Foo {
  Bar bar;

  @Inject
  public Foo(Bar bar) {
     bar.bar(); // <-- would work as long as TP knows how to create the instance of Bar 
  }
  • use a @Inject annnotated method. These methods are executed after fields are initialized.
class Foo {
  @Inject Bar bar;

  @Inject
  public Foo() {
     bar.bar(); // <-- would fail because members are not initialized yet, see the factory code.
  }

 @Inject
 void init() {
     bar.bar(); // <-- would work
  }
}

@stephanenicolas
Copy link
Owner

Release 1.0.8 will solve this issue.

stephanenicolas added a commit that referenced this issue Aug 22, 2017
After merging fixes for #196 and #227
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants