1

Background:
I am working on a resource extensive operation and reinitialising the object is too costly operation. So the current implementation involves following steps

Initialisation -> Usage -> Cleanup

Say my code is something like

public class A {

    public A(){
        //perform resource extensive operations
    }

    public A performTask(){
        //task performed
        return this;
    }

    public void saveChanges(){
        //perform commits and save changes
        //cleanup resource
        //perform other minor tasks
    }
}

Requirement:
Now how to inform the person using this class to call saveChanges() method after calling performTask(). Please note that performTask() is a dummy reference in this example, and in actual code it can be called multiple times.

Best example for my use case Android Shared Preference Editor. If I don't call commit() or apply() after calling edit() I get a warning like below

SharedPreferences.edit() called without a corresponding commit() or apply()

So how can I create a warning something like above, To make sure saveChanges() is called after performTask()?

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • Why are you not calling saveChanges() in the body of performTask() itself? Is it the case that saveChanges() must be called after a certain number of performTask() calls? – Solace Sep 23 '18 at 07:32
  • @Solace yes, multiple calls will be made to performTask based on API response before calling save changes. – Mohammed Atif Sep 23 '18 at 08:10

0 Answers0