yigit / android-priority-jobqueue

A Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
3.4k stars 395 forks source link

Persists job. Injected object is null #83

Closed bkodirov closed 8 years ago

bkodirov commented 8 years ago

import com.path.android.jobqueue.Params;

import javax.inject.Inject;

import uz.lamuz.player.PlayerApp; import uz.lamuz.player.data.api.service.LikeService; import uz.lamuz.player.jobqueue.BaseJob; import uz.lamuz.player.jobqueue.Groups; import uz.lamuz.player.jobqueue.Priority;

/**

I use Dagger2. Injected field are always null. If remove .persist() it executes without exceptions. I presume it might because of LikeService interface(it is reference by interface). What is the cause of this NPE?

yigit commented 8 years ago

Who is calling onInject ? Job manager does not have this, did you attach the Injector similar to the dev summit demo does? https://github.com/yigit/dev-summit-architecture-demo/blob/master/client/app/src/main/java/com/android/example/devsummit/archdemo/di/module/ApplicationModule.java#L109

bkodirov commented 8 years ago

I'm using Dagger2 by google. onInject is called by super class in the onAdded method.

yigit commented 8 years ago

That wont work because for a persistent job, the instance which receives the onAdded call is different form the instance that runs. It may even run another time when app is restarted etc.

To properly hook your injector, JobManager provides a dependency injection API: https://github.com/yigit/android-priority-jobqueue/wiki/Job-Manager-Configuration

See the example I linked above.

bkodirov commented 8 years ago

@yigit You can not inject Base class on Dagger2 unfortunally. You have to go again your injector logic for dagger2. You can not inject like this https://github.com/yigit/dev-summit-architecture-demo/blob/master/client/app/src/main/java/com/android/example/devsummit/archdemo/di/module/ApplicationModule.java#L109 on Dagger2. Because, Dagger2 no longer supports parent injecting.

yigit commented 8 years ago

That example uses dagger2 :). Yes dagger2 made JobManager's API ugly but the way it works in that demo is that it calls the job to call the injector :(. To the injector is not called with the base class.

So the base method in the job has inject method that receives the app component. Then each job that wants to use the injector override that method to call the injector on themselves. FetchFeedJob

I re-opened the issue. Feel free to close if it is clear or if not, please provide why your use case does not work as in the sample app (which also use Dagger2).

yigit commented 8 years ago

closing due to inactivity + we already have an example of using this with dagger2.