Skip to content
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

Merged
merged 1 commit into from
Dec 6, 2020

Conversation

byencho
Copy link

@byencho byencho commented Nov 22, 2020

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 calling Base64.encodeToString (#48).

The original need for Base64 encoding was to get around the limitation that SharedPreferences could not be used to store a raw byte[], so the closest thing we could get was to encode the data and store it as a String. 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 of SharedPreferences: 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.

mDiskHandler = new FileDiskHandler(context, mExecutorService);

// Clear out any data from old storage mechanism
mSharedPreferences.edit().clear().apply();
Copy link

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?

Copy link
Author

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);

Copy link

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).

Copy link
Author

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.

Copy link
Author

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.
Copy link

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Author

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the word not

Copy link

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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@byencho byencho merged commit 5845e01 into master Dec 6, 2020
@byencho byencho deleted the improve-base64-encoding branch December 6, 2020 20:37
@byencho
Copy link
Author

byencho commented Dec 6, 2020

Thanks @weisers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants