Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Application not responding after adding an entry to the queue #188

Open
evatix-android opened this issue Mar 29, 2018 · 2 comments
Open

Comments

@evatix-android
Copy link

After adding a task into the TaskQueue, application freezes eventually causes an ANR.
TaskQueue is extended to a subclass. When the add() method is called from this subclass using super this scenario happens.

@JakeWharton
Copy link
Collaborator

Are you interacting with it on the main thread?

Without some kind of executable sample it's going to be nearly impossible to know what's going on here.

@maruf047
Copy link

Hello Jake, i am posting the coding scenario here.

Queue is created by injection using Dagger. The add() call is made in main thread. Application stops at super.add(entry) in add(SampleTask entry) of SampleTaskQueue class.

DaggerModule.java

    @Provides @Singleton
    SampleTaskQueue provideTaskQueue(Gson gson) {
        return SampleTaskQueue.create(app.getApplicationContext(), gson);
    }

SampleFragment.java

    ...

    @Inject
    protected SampleTaskQueue queue;

    ...

    void addTask(){ 
        queue.add(new SampleTask( params ... ));
    }

    ...

SampleTask.java

public class SampleTask implements Task<SampleTask.Callback> {

    public SampleTask( params ... ) {
        // Some initialization ...
    }

    @Override
    public void execute(Callback callback) {
        // Some task ...
    }

    interface Callback {
        void onSuccess();
        void onFailure();
    }

}

SampleTaskService.java

public class SampleTaskService extends Service implements SampleTask.Callback {

    @Inject
    SampleTaskQueue queue;

    private boolean isRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        ((SampleApplication) getApplication()).inject(this);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        executeNext();
        return START_STICKY;
    }

    private void executeNext() {

        // Only one task at a time
        if (isRunning) return;

        SampleTask task = queue.peek();
        if (task != null) {
            isRunning = true;
            task.execute(this);
        } else {
            stopSelf();
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onSuccess() {
        isRunning = false;
        queue.remove();
        executeNext();
    }

    @Override
    public void onFailure() {
        isRunning = false;
    }

}

SampleTaskQueue.java

public class SampleTaskQueue extends TaskQueue<SampleTask> {

    private static final String FILENAME = "sample_task_queue";

    private Context context;

    public SampleTaskQueue(ObjectQueue<SampleTask> delegate, Context context) {
        super(delegate);
        this.context = context;

        if (size() > 0) {
            startService();
        }
    }

    private void startService() {
        context.startService(new Intent(context, SampleTaskService.class));
    }

    @Override
    public void add(SampleTask entry) {
        super.add(entry); // Application stops here
        startService();
    }

    @Override
    public void remove() {
        super.remove();
    }

    public static SampleTaskQueue create(Context context, Gson gson) {
        FileObjectQueue.Converter<SampleTask> converter = new GsonConverter<>(gson, SampleTask.class);
        File queueFile = new File(context.getFilesDir(), FILENAME);
        FileObjectQueue<SampleTask> delegate;
        try {
            delegate = new FileObjectQueue<>(queueFile, converter);
        } catch (IOException e) {
            throw new RuntimeException("Unable to create file queue.", e);
        }
        return new SampleTaskQueue(delegate, context);
    }
    
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants