2017-12-27 21 views
0

Android 용 Dagger2 라이브러리가있는 Singleton에 문제가 있습니다. 디스패처를 제공하는 DispatcherModule 클래스가 포함Android Dagger2, Singleton cross 구성 요소

DispatcherComponent : 내 문제는 @Singleton을 사용하지만, 두 개의 서로 다른 개체 =를 [

내가 2 개 구성 요소 및 2 개 모듈이 점점 메신저입니다. Dispatcher는 StoreModule에서 제공하는 인스턴스 UserStore를 필요로합니다.

@Singleton 
@Component(modules={AppModule.class, StoreModule.class, DispatcherModule.class}) 
public interface DispatcherComponent { 
    void inject(SomeClass someClass); 
} 

및 메신저하려고 할 때

@Module 
public class DispatcherModule { 
    @Provides 
    @Singleton 
    public Dispatcher provideDispatcher(UserStore store) { 
     Log.d("DEBUG", "userStore : " + store.toString()); 
     return new Dispatcher(store); 
    } 

을 다음과 지금 StoreComponent.class

@Singleton 
@Component(modules={AppModule.class, StoreModule.class}) 
public interface StoreComponent { 
    void inject(SomeOtherClass otherClass); 
} 

및 StoreModule.class 지금

@Module 
public class StoreModule { 
    @Provides 
    @Singleton 
    public UserStore provideUserStore() { 
     return new UserStore(); 
    } 

로 DispatcherModule.class입니다 사용자를 삽입하는 방법 메신저 재 두 개의 서로 다른 개체 =/

public class SomeOtherClass extends Acitivity { 
    @Inject UserStore mStore; 
    public void onCreate(Bundle savedInstance) { 
     StoreComponent comp = ((MyApp) getApplication).getStoreComponent(); 
     comp.inject(this); 
     Log.d("DEBUG", "userStore2 :" + mStore.toString()); 
     } 
} 

public class SomeClass { 
    @Inject Dispatcher mDispatcher; 
    public SomeClass (Application application) { 
     ((MyApp) application).getDispatcherComponent().inject(this); 
    } 

과 마지막을 받고,이 내가 구성 요소를 만드는 방법은 다음과 같습니다 지금

public class MyApp extends Application { 
    public void onCreate() { 
     StoreModule store = new StoreModule(); 
     StoreComponent storeComponent = DaggerStoreComponent.builder().appModule(new AppModule(this)).storeModule(storeModule).build(); 
     DispatcherComponent disComp = DaggerDispatcherComponent.builder().appModule(new AppModule(this)).storeModule(storeModule).dispatcherModule(new DispatcherModule()).build(); 
} 

, 메신저 응용 프로그램을 실행할 때, 내가이 다른 개체를 얻을! 누군가 나를 도울 수 있습니까? 어떻게 고칠 수 있니? 나는 신 구성 요소를 갖고 싶지 않다 ..

THanks!

답변

0

@Singleton dost는 객체가 실제로는 싱글 톤이 아니라 대신 단검에 의해 생성 된 구성 요소 impl이 보유하는 캐시에 DoubleCheck 클래스를 사용합니다. 자세한 내용은 DaggerDispatcherComponent를 확인하십시오.

이 경우를 들어

, 아래처럼 StoreModule을 변경할 수 있습니다

@Module 
public class StoreModule { 
    private UserStore mStore; 

    @Provides 
    @Singleton 
    public UserStore provideUserStore() { 
     return getStore(); 
    } 

    private UserStore getStore() { 
     //lazy initialized and make sure thread safe 
     if (null == mStore) { 
      @synchronized(StoreModule.class) { 
       if (null == mStore) { 
        mStore = new UserStore(); 
       } 
      } 
     } 
     return mStore; 
    } 
} 
+0

감사합니다! 그것은 작동합니다 :) 이제는 분명합니다. Singleton은 동일한 구성 요소 권한을 사용하는 경우에만 실제 싱글 톤입니까? – user3534500

+0

실제 싱글 톤은 정적 인스턴스가 아니라 컴포넌트 인스턴스의 멤버 일뿐, 일단 컴포넌트가 GC에 의해 릴리즈되면 저장소도 해제되어 사실상 컴포넌트에 의해 userStore가 유지됩니다. – luffy