Closed saket closed 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 .
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.
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 .
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 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 .
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!
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.