0

I have a Room entity as so:

@Entity(tableName = "user")
public class User {

    @PrimaryKey
    @ColumnInfo(name = "id")
    private int id;

    @ColumnInfo(name = "name")
    private String name;

    @ColumnInfo(name = "age")
    private int age;

    public User() {}

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

and ViewModel as so:

public class MainViewModel extends AndroidViewModel {
    
    public MainViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public LiveData<List<User>> fetchLiveUsers() {
        return repository.fetchUsers();
    }
    
}

and observing the LiveData as so:

mainViewModel
        .fetchLiveUsers()
        .observe(getViewLifecycleOwner(), new Observer<List<User>>() {
            @Override
            public void onChanged(List<User> userList) {
                if(userList != null)
                    usersAdapter.setModelsList(userList);
            }
        });

When I have a large dataset of users, such as 1000+ users, and I'm observing this live data, any changes in the database will trigger the onChanged() method.

Does this mean that each time onChanged() is called, the 1000+ users get loaded from the Room database, or does the updated user only get loaded from the database to update the current User list being observed by livedata?

AtomicallyBeyond
  • 348
  • 1
  • 15
  • Yes, this will load all users from the database again. but for 1k to 10k users, this isn't too heavy to reload in my opinion. – Gulab Sagevadiya May 07 '22 at 03:22
  • So if you were dealing with a larger and complex object, or larger dataset, would you still uses livedata? – AtomicallyBeyond May 07 '22 at 03:26
  • no pagination is more useful in this case. – Gulab Sagevadiya May 07 '22 at 03:27
  • I have implemented live data with more then 2k complex object dataset with live data works fine in lower computation power device nicely – Gulab Sagevadiya May 07 '22 at 03:29
  • Does this answer your question? [Room : LiveData from Dao will trigger Observer.onChanged on every Update, even if the LiveData value has no change](https://stackoverflow.com/questions/47215666/room-livedata-from-dao-will-trigger-observer-onchanged-on-every-update-even-i) – MDT May 07 '22 at 03:31
  • @gulab patel so if a database contains 1000 rows, and you update a single row, the whole database gets loaded again? Isn't there a way just to get the single row? – AtomicallyBeyond May 08 '22 at 06:08

0 Answers0