Load from cache and refresh

Sometimes we want to display local data if available, but still hit the server to get the latest data available. Livebox comes with a refresh()option to cover this use case.

// Builds an instance of Livebox using LiveboxBuilder class.
Livebox<UsersRes, Users> usersBox = new LiveboxBuilder<UsersRes, Users>()
                    .withKey("get_users")
                    .fetch(api::getUserList, UsersRes.class)
                    .addSource(Sources.MEMORY_LRU, ageValidator)
                    .addSource(Sources.DISK_PERSISTENT, persistentDiskValidator)
                    .addConverter(UsersRes.class, usersRes -> Optional.of(Users.fromUsersRes(usersRes)))
                    .retryOnFailure()
                    .refresh(true)
                    .build();

When the option .refresh() is used, even if there's valid data stored locally we still hit the server to refresh it. In this case the Observable will emit twice, first with the local data, that can be used to immediately show content to the user and then with remote data received from fetcher.

Last updated