0

Like this:

Proc is a functional interface type void.

public class MainViewPagerViewModel extends ViewModel {

    private HashMap<Integer, Integer> quantities = new HashMap<>();
    private WeakReference<Proc> weakProc;

    public void put(Integer position, Integer quantity) {
        quantities.put(position, quantity);
        if (weakProc.get() != null) {
            weakProc.get().run();
        }
    }

    public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
        weakProc = new WeakReference<>(
                () -> mapConsumer.accept(quantities)
        );
    }

}

Will this work, or I'm I still holding reference to the Consumer's onwer? or... is this too much? maybe this is enough?

    public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
        
            proc = () -> mapConsumer.accept(quantities)
      
    }

Thanks in advance.

Delark
  • 1,141
  • 2
  • 9
  • 15
  • _Proc is a functional interface type void_ - you mean `Runnable`? can you make it clear in the question? This entire question makes no sense, can you explain in plain english what you want to achieve? – Eugene Sep 11 '20 at 16:29

1 Answers1

1

First of all, there is a very general advice that needs to be in place here: unless you know what some constructs do (WeakReference in this case), don't use it. Study it's documentation, run more than just one example and when you are comfortable with it - use it. WeakReferences are a sharp tool, and you need to know all the problems and issue you might encounter. Here are a few intros about what it is here, or here or may be even here.

And now - what are you trying to solve, specifically? Is your code broken without that wrapper around Proc: private WeakReference<Proc> weakProc;? I don't think it is.

By introducing that WeakReference it's like you are saying that Proc (which is a Runnable defined as a lambda expression) will be GCed, while the MainViewPagerViewModel is still alive. And now ask yourself - is that even possible? Forget about WeakReference here for a second. Can proc be garbage collected at some point in time while MainViewPagerViewModel is still going to be alive? If that is the case - what would happen when you would call put?

When you have the answer to the above, you will understand that your question makes somehow very little sense; and the introduction of WeakReference there is useless.

Eugene
  • 117,005
  • 15
  • 201
  • 306
  • Yes, it does make little to no sense, ViewModel will survive the class consuming the hashmap... definitely. I think my confusion is a matter of meaning/definition. Is it _holding_ reference, or to _be held_ in a reference, who is the responsible and who is the affected, what is the cause of an effect that involves 2 equally synergistic environments, where the consumption of one is the production of another (another concept that I cannot fully grasp) My concern is that the class where the consumer is created new is held by the viewmodel that will survive it. – Delark Sep 11 '20 at 18:29
  • 1
    @Delark Ive read your comment a few times, but I can't make much sense out of it. What if you try to break things down, to smaller questions/examples? – Eugene Sep 12 '20 at 03:44
  • I dont know how to ask... Something tells me that there may be a situation where Consumers/Functions, and or listeners, may cause Memory leaks and I don't know which or how. – Delark Sep 12 '20 at 04:25