f2prateek / rx-preferences

Reactive SharedPreferences for Android
http://f2prateek.com/2015/10/05/rx-preferences/
Apache License 2.0
1.54k stars 132 forks source link

Usecase for getAll() #111

Closed saket closed 6 years ago

saket commented 6 years ago

Hey folks, does rx-preferences intentionally not include a way to observe all items? If not, I'd like to contribute by sending a PR.

JakeWharton commented 6 years ago

With what type would you do this? And what's the use case?

On Fri, Jan 5, 2018, 10:53 PM Saket Narayan notifications@github.com wrote:

Hey folks, does rx-preferences intentionally not include a way to observe all items? If not, I'd like to contribute by sending a PR.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/f2prateek/rx-preferences/issues/111, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEQ4hFd9nJjcUYaAI2LbDCCT2lQq6ks5tHu5RgaJpZM4RVLwQ .

saket commented 6 years ago

My use case was to observe all changes and get a stream of Map<String, ?>. Although doing it manually requires only 5-6 lines of code, I was hoping to keep things consistent and do it through rx-preferences.

JakeWharton commented 6 years ago

Wouldn't it be Pair<String, ?> since the listener only delivers changes one at a time regardless if you update 1 or 100 in a single edit.

On Sun, Jan 7, 2018 at 12:32 AM Saket Narayan notifications@github.com wrote:

My use case was to observe all changes and get a stream of Map<String, ?>. Although doing it manually requires only 5-6 lines of code, I was hoping to keep things consistent and do it through rx-preferences.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/f2prateek/rx-preferences/issues/111#issuecomment-355801003, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEf-5fgosgEqPIXEYHG3dhvIJMit-ks5tIFb0gaJpZM4RVLwQ .

saket commented 6 years ago

Sure, Pair<String, ?> if you want to receive the changes individually, but the callback from shared preference's listener can also be mapped to a map of all items present in the file.

JakeWharton commented 6 years ago

You want the entire listing? I'm not sure we should encourage that... The simple version of that is going to create a lot of a GC garbage creating the map each time. A correctly implemented version requires either a persistent data structure so that when a single entry changes we don't create an entire new map, or it requires a custom Map subtype which is a facade over the backing SharedPreferences object to try and do as much lazily as possible.

On Sun, Jan 7, 2018 at 12:52 AM Saket Narayan notifications@github.com wrote:

Sure, Pair<String, ?> if you want to receive the changes individually, but the callback from shared preference's listener can also be mapped to a map of all items present in the file.

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/f2prateek/rx-preferences/issues/111#issuecomment-355801685, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEeSF5QM6rNHR0njCJvllc1JlxGScks5tIFuKgaJpZM4RVLwQ .

saket commented 6 years ago

Apologies for replying late. Allocating a map on every change does sound very expensive. I just realized that SharedPreferences does not create an immutable copy of its map to avoid this problem.

A persistent data structure or a custom Map subtype sounds more effort than simply bypassing rx-preferences and implementing a change listener manually.

I'll close this issue, thanks for replying!