Giter Site home page Giter Site logo

Comments (2)

JakeWharton avatar JakeWharton commented on July 28, 2024

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.

from tape.

maruf047 avatar maruf047 commented on July 28, 2024

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);
    }
    
}

from tape.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.