Giter Club home page Giter Club logo

Comments (5)

JonatanSalas avatar JonatanSalas commented on September 27, 2024 1
  1. Yes, it holds the Presenter reference, but it's not a strong reference. I have been using this approach in about three apps I have made, those
    apps were analyzed using Canary Leak from Square and it passed well.

  2. About this one, you are on the right, I have to analyze a little bit the performance impact of this.

I will take a look deep further the next week, and I will tell you about it. This can be improved, sure.

Thank you very much for your suggestions!

from mvp-helpers.

JonatanSalas avatar JonatanSalas commented on September 27, 2024

I have been looking a little about 2. Seems that the correct way to implement the method run in background to be optimized it's this one:

public class BaseInteractor {

   protected void runInBackground(@NonNull Runnable runnable) {
       Preconditions.checkNotNull(runnable, "Runnable shouldn't be null");
       final ExecutorService executor = Executors.newFixedThreadPoolExecutor(5);
       
       executor.submit(runnable);

       if (!executor.isShutdown()) { 
           executor.shutdown();
       } 
    }
       .....
}

Today I will change the BaseInteractor class!

from mvp-helpers.

JonatanSalas avatar JonatanSalas commented on September 27, 2024

This is the new approach in BaseInteractor class:

public class BaseInteractor {
    private static final int NUMBER_OF_THREADS = 5;

    private ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(NUMBER_OF_THREADS);
    private ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_THREADS);
    private Handler handler = new Handler(Looper.getMainLooper());

    private ScheduledFuture<?> scheduledFuture;

    protected void runOnUiThread(@NonNull Runnable runnable) {
        Preconditions.checkNotNull(runnable, "runnable shouldn't be null");

        handler.post(runnable);
    }
    
    protected void runOnBackground(@NonNull Runnable runnable) {
        Preconditions.checkNotNull(runnable, "runnable shouldn't be null");

        if (executor.isTerminated()) {
            executor = Executors.newFixedThreadPool(5);
        }

        final Future<?> task = executor.submit(runnable);

        if (task.isDone()) {
            executor.shutdown();
        }
    }

    protected void runScheduledTaskOnBackground(@NonNull Runnable runnable, @NonNull Long time, @NonNull TimeUnit unit) {
        Preconditions.checkNotNull(runnable, "runnable shouldn't be null");
        Preconditions.checkNotNull(time, "time shouldn't be null");
        Preconditions.checkNotNull(unit, "unit shouldn't be null");

        scheduledFuture = scheduledExecutor.schedule(runnable, time, unit);
    }
    
    protected void cancelScheduledTask() {
        if (null != scheduledFuture) {
            scheduledFuture.cancel(true);
        }
    }
}

Moved from Executor to ExecutorService, in order to make easy to reuse the Thread Pool.

from mvp-helpers.

JonatanSalas avatar JonatanSalas commented on September 27, 2024

@jtyang If you want to probe this new approach you can do it, by following the steps from here https://jitpack.io/#BlackBoxVision/mvp-helpers/-SNAPSHOT

from mvp-helpers.

JonatanSalas avatar JonatanSalas commented on September 27, 2024

@jtyang Closing this issue since there's no activity. If you need something else please, re open it, or create a new issue! 😄

from mvp-helpers.

Related Issues (6)

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.