Skip to content

Should we use instance variables inside a ViewModel to reuse fetched data? #84

Open
@hernandazevedozup

Description

@hernandazevedozup

What is the best approach?

// Only expose livedata
public class MyViewModel extends ViewModel {
    private MutableLiveData<List<User>> users = new MutableLiveData();
    public void loadUsers() {
        // Do an asynchronous operation to fetch users.
        users.postValue(/* ---- userList  --- */);
    }
}
// Expose livedata and instance variable userList
public class MyViewModel extends ViewModel {
    private MutableLiveData<List<User>> users = new MutableLiveData();
    private List<User> userList;
   
   //Expose already fetched list 
    public List<User> getUserList() {
     return userList;
    }

    public void loadUsers() {
        // Do an asynchronous operation to fetch users.
        this.userList = userList;
        users.postValue(/* ---- userList  --- */);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions