-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use custom file storage solution to avoid Base64 encoding #62
Conversation
mDiskHandler = new FileDiskHandler(context, mExecutorService); | ||
|
||
// Clear out any data from old storage mechanism | ||
mSharedPreferences.edit().clear().apply(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any plan to remove this at some point? Or are you just going to leave this here indefinitely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good question. I'm not exactly sure the best way to go about getting rid of this. I would think that I'd have to keep it around for at least a few versions and then make a note when I finally remove it.
@@ -204,6 +216,7 @@ public void run() { | |||
} | |||
mPendingWriteTasks.add(runnable); | |||
mExecutorService.execute(runnable); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to remove this new extra line (unless for organizational purposes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll double check, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this line but also used this as an opportunity to replace mExecutorService.execute(runnable)
with doInBackground(runnable)
.
* Stores the given {@code bytes} to disk and associates them with the given {@code key} for | ||
* retrieval later. | ||
* | ||
* @param key The to associate with the saved data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing word between The
and to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
private final Map<String, byte[]> mKeyByteMap = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Determines whether or the {@link #waitForFilesToLoad()} call has completed in some way or |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing the word not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(or take out or the
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
3f1492f
to
10ef326
Compare
10ef326
to
c81c485
Compare
c81c485
to
bd97329
Compare
Thanks @weisers |
This PR replaces the usage of
SharedPreferences
with a custom file solution in order to avoid the need to Base64 encode the data before persisting it. This is to address OOM issues that can arise when callingBase64.encodeToString
(#48).The original need for Base64 encoding was to get around the limitation that
SharedPreferences
could not be used to store a rawbyte[]
, so the closest thing we could get was to encode the data and store it as aString
. With a custom file solution we can simply write the bytes directly and avoid the extra, memory-intensive encoding step. While I tried to keep my implementation (FileDiskHandler
) as simple as possible, I did add a small feature that matches some of the behavior ofSharedPreferences
: on startup, all of the file data will be read into memory on a background thread and the first call to retrieve data will block (up until a cutoff) until data has been read. This just helps ensure that all disk handling is front-loaded so that we don't block the main thread when navigating around and restoring data.