Dim
Dim

Reputation: 4837

Correct way for Singleton usage in class with static functions and callbacks

I have an DB manager class for my queries which runs with Asynctask:

public class DBManager {

    private static DBCallback dbCallback;

    //I need this for callbacks to main class when operation is finished
   public DBManager(DBCallback mClass) {

        this.dbCallback = mClass;

    }


    public static void getAllUsers() {
        new AsyncTask<Void, Void, List<UserDB>>() {
            @Override
            protected List<UserDB> doInBackground(Void... voids) {

                return DatabaseClient.getInstance(ApplicationContextProvider.getContext()).getAppDatabase().userDao().getAll();
            }

            @Override
            protected void onPostExecute(List<UserDB> users) {
                super.onPostExecute(users);

                dbCallback.finishedReadFromDB(users); //Sending callback

            }
        }.execute();
    }

    public static void deleteUserLocal(final UserDB user) {
    new AsyncTask<UserDB, Void, Void>() {
        @Override
        protected Void doInBackground(UserDB... users) {

            DatabaseClient.getInstance(ApplicationContextProvider.getContext()).getAppDatabase().userDao().delete(users[0]);
            return null;
        }
    }.execute(user);
}
}

At my MainClass I am using

dbManager = new DBManager(this);

for receiving callback, so I am using

dbManager.getAllUsers();

and then gets callback when operation is finished.

But I have fictions where I do not need to return anything like deleteUserLocal. So I can user ether

dbManager.deleteUserLocal(user)

or

DBManager.deleteUserLocal(user)

due to that the function is static.

From classes that not require callback of course I using

DBManager.deleteUserLocal(user)

So... I do not like that every time at onCreate am I am crating new instnce of DBManager:

dbManager = new DBManager(this);

Just for callbacks. How can I create a singleton class which I can use for callback and only use

DBManager.getAllUsers(); instead of dbManager.getAllUsers();

Upvotes: 0

Views: 373

Answers (2)

Alexander Dauer
Alexander Dauer

Reputation: 51

You may use this pattern

https://www.google.de/amp/s/www.geeksforgeeks.org/singleton-design-Patient tern/amp/

And then work with get instance.

But I would consider to implement the asyncTask without a return value if you are already using Callbacks already.

Try this link

https://de.m.wikibooks.org/wiki/Muster:_Java:_Singleton

Upvotes: 1

Greg Moens
Greg Moens

Reputation: 1825

To achieve what you want, get rid of the constructor and change the static callback to public. That way you can set the public static variable externally before calling any of the other static methods. A constructor in a class with all static members is unnecessary.

Now with that being said, this design for accessing a database in Android is not going to scale very well. DBManager will only be able to have one client at any given time. A better approach would be to pass in the callback for any method that requires it and drop the static variable callback.

Upvotes: 1

Related Questions