Last active
September 29, 2015 22:42
-
-
Save frogermcs/102c7097eabd340195ae to your computer and use it in GitHub Desktop.
Dagger 2 performance improvements source code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HeavyExternalLibrary { | |
private boolean initialized = false; | |
public HeavyExternalLibrary() { | |
} | |
public void init() { | |
try { | |
Thread.sleep(500); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
initialized = true; | |
} | |
public void callMethod() { | |
if (!initialized) throw new RuntimeException("Call init() before you use this library"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HeavyLibraryWrapper { | |
private HeavyExternalLibrary heavyExternalLibrary; | |
private boolean isInitialized = false; | |
ConnectableObservable<HeavyExternalLibrary> initObservable; | |
public HeavyLibraryWrapper() { | |
initObservable = Observable.create(new Observable.OnSubscribe<HeavyExternalLibrary>() { | |
@Override | |
public void call(Subscriber<? super HeavyExternalLibrary> subscriber) { | |
HeavyLibraryWrapper.this.heavyExternalLibrary = new HeavyExternalLibrary(); | |
HeavyLibraryWrapper.this.heavyExternalLibrary.init(); | |
subscriber.onNext(heavyExternalLibrary); | |
subscriber.onCompleted(); | |
} | |
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).publish(); | |
initObservable.subscribe(new SimpleObserver<HeavyExternalLibrary>() { | |
@Override | |
public void onNext(HeavyExternalLibrary heavyExternalLibrary) { | |
isInitialized = true; | |
} | |
}); | |
initObservable.connect(); | |
} | |
public void callMethod() { | |
if (isInitialized) { | |
HeavyExternalLibrary.callMethod(); | |
} else { | |
initObservable.subscribe(new SimpleObserver<HeavyExternalLibrary>() { | |
@Override | |
public void onNext(HeavyExternalLibrary heavyExternalLibrary) { | |
heavyExternalLibrary.callMethod(); | |
} | |
}); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void setupActivityComponent() { | |
Debug.startMethodTracing("SplashTrace"); | |
GithubClientApplication.get(this) | |
.getAppComponent() | |
.plus(new SplashActivityModule(this)) | |
.inject(this); | |
Debug.stopMethodTracing(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//AppModule | |
@Provides | |
@Singleton | |
HeavyExternalLibrary provideHeavyExternalLibrary() { | |
HeavyExternalLibrary heavyExternalLibrary = new HeavyExternalLibrary(); | |
heavyExternalLibrary.init(); | |
return heavyExternalLibrary; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SplashActivityModule | |
@Provides | |
@ActivityScope | |
SplashActivityPresenter | |
provideSplashActivityPresenter(Validator validator, UserManager userManager, HeavyExternalLibrary heavyExternalLibrary) { | |
return new SplashActivityPresenter(splashActivity, validator, userManager, heavyExternalLibrary); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment