Skip to content

Use Commit Over Apply#295

Merged
mCodex merged 2 commits intomCodex:masterfrom
connectedbits:commit-changes
Jun 27, 2021
Merged

Use Commit Over Apply#295
mCodex merged 2 commits intomCodex:masterfrom
connectedbits:commit-changes

Conversation

@netshade
Copy link
Contributor

@netshade netshade commented Jun 9, 2021

Found while researching some odd behavior on customer's phones:

When writing the encrypted value to the user's shared preferences, it appears that currently the change is written using .apply. Based on the nature of the data being written, it seems like the more correct method to use here is .commit. Given that, for a number of reasons, the value may fail to actually be written to the disk, the caller would likely want to know if it failed, as well as to not perform any future computation until the write has definitely either failed or succeeded.

I had some trouble actually exercising the failure paths here when trying to force setItem to fail ( I tried, in the emulator, marking preference XML files as 0400, corrupting them, etc. ) but it seems that SharedPreferences just overwrites the files at boot up. If you have any ideas on how to better test this, would be helpful. In any case, I think the semantics change here is the correct one, even if it will reduce performance using setItem.

@netshade
Copy link
Contributor Author

netshade commented Jun 9, 2021

Also, apologies, my local copy of prettier reformatted the code in the example TS. If you'd prefer, I can back out the formatting changes to stay consistent w/ prior.

Copy link
Owner

@mCodex mCodex left a comment

Choose a reason for hiding this comment

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

Many thanks!!!

Don't worry about prettier stuff, I'll update the example code.

@mCodex mCodex merged commit 2ad64e4 into mCodex:master Jun 27, 2021
@netshade netshade deleted the commit-changes branch June 27, 2021 18:22
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.

2 participants

Comments