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

get jobs qeues list return null #351

Closed pishguy closed 7 years ago

pishguy commented 7 years ago

findJobsWith return null :

private void getRepositories() {
    jobManager.addJobInBackground(new GetLatestRepositories(getSessionId()));
    Set<JobHolder> jobs = findJobsWith(getSessionId());
}
protected static Set<JobHolder> findJobsWith(final String tag) {
    Constraint constraint = new Constraint() {
        @Override
        public Set<String> getTags() {
            HashSet<String> strings = new HashSet<>();
            strings.add(tag);
            return strings;
        }

        @Override
        public TagConstraint getTagConstraint() {
            return TagConstraint.ANY;
        }

        @Override
        public int getMaxNetworkType() {
            return NetworkUtil.UNMETERED;
        }
    };

    return persistentQueue.findJobs(constraint);
}

JobManagerModule

@Module(includes = {ContextModule.class, SyncQueueModule.class})
public class JobManagerModule {

    private ApplicationComponent component;

    @Provides
    @AlachiqApplicationScope
    public JobManager jobManager(@ApplicationContext Context context) {
        component = DaggerApplicationComponent.builder()
                .githubApplicationComponent(Alachiq.getComponent())
                .build();
        Configuration.Builder builder = new Configuration.Builder(context)
                ...
                .injector(new DependencyInjector() {
                    @Override
                    public void inject(Job job) {
                        if (job instanceof JobManagerInjectable) {
                            ((JobManagerInjectable) job).inject(component);
                        }
                    }
                }).queueFactory(new DefaultQueueFactory() {
                    @Override
                    public JobQueue createPersistentQueue(Configuration configuration, long sessionId) {
                        persistentQueue = new SyncQueue(super.createPersistentQueue(configuration, sessionId));
                        return persistentQueue;
                    }
                });

        ...
        return new JobManager(builder.build());
    }
}

Application Class:

public class Alachiq extends MultiDexApplication {
    private static Context                    context;
    public static  SlidrConfig                config;
    private        GithubService              githubService;
    private        Picasso                    picasso;
    private static GithubApplicationComponent component;
    private        JobManager                 jobManager;
    private        Bus                        getRxBus;
    private static Alachiq                    instance;
    public static  JobQueue                   persistentQueue;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        ...
        component = DaggerGithubApplicationComponent.builder()
                .contextModule(new ContextModule(this))
                .jobManagerModule(new JobManagerModule())
                .networkServiceModule(new NetworkServiceModule("https://api.github.com/"))
                .build();

        githubService = component.getGithubService();
        picasso = component.getPicasso();
        jobManager = component.getJobManager();
    }

    public static GithubApplicationComponent getComponent() {
        return component;
    }
    ...
}

and then GetLatestRepositories to use JobQueue

public class GetLatestRepositories extends Job implements JobManagerInjectable {
    @Inject
    transient GithubService githubService;

    @Inject
    transient Bus eventBus;

    private Call<List<GithubRepo>> repositoryCall;

    public GetLatestRepositories(String sessionId) {
        super(new Params(JobPriority.MID).addTags(sessionId).requireNetwork().persist());
    }

    @Override
    public void onAdded() {
        eventBus.register(this);
    }

    @Override
    public void onRun() throws Throwable {
        ...
    }

    @Override
    protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
        eventBus.unregister(this);
    }

    @Override
    protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
        return null;
    }

    @Override
    public void inject(ApplicationComponent component) {
        component.inject(this);
    }
}
yigit commented 7 years ago

you are using a custom queue, not related to job manager.

pishguy commented 7 years ago

@yigit

so how can i get list?

gabrielmoreira commented 7 years ago
  1. Your onRun method code, is an asynchronous call?
  2. Can you test using jobManager.addJob(new GetLatestRepositories(getSessionId())); ?
pishguy commented 7 years ago

hi @gabrielmoreira thanks to reply, i'm not sure its asynchronous call

using jobManager.addJob(new GetLatestRepositories(getSessionId())); work fine without problem, but i cant get job status, did you test how can i get job status?