Skip to content

AsyncStorage.multiRemove method has different behavior on Android and iOS #6

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

Closed
jhoobergs opened this issue Feb 13, 2019 · 12 comments
Closed
Labels
bug Something isn't working help wanted :octocat:

Comments

@jhoobergs
Copy link

When calling the AsyncStorage.multiRemove method with an empty array, the behavior on Android and iOS is different:

I created this issue at the facebook/react-native repo: facebook/react-native#23412 where I was told to create it at this repo.

@krizzu
Copy link
Member

krizzu commented Feb 13, 2019

Thanks @jhoobergs for raising this issue. Good catch!
I think we should throw an error on iOS too, to have the same behavior on both platforms. Would like to open a PR with fix for that?

@krizzu krizzu added the bug Something isn't working label Feb 13, 2019
@hawkrives
Copy link

I personally don't see why calling multiRemove with an empty array should be an error - it just means that I'm not asking it to remove anything.

I'm curious as to why you feel that throwing an error is the correct default?

I agree that we should align the platforms!

@jhoobergs
Copy link
Author

The current error message on Android is also a bit strange: "Error: Invalid key" without a key name because there is no key. We got a lot of these errors in our sentry and couldn't figure out where they came from.

I think @hawkrives is right about not throwing an error on empty arrays.

@pbitkowski
Copy link
Contributor

I prefer to get feedback about actions. If not get an error then maybe boolean value would be a way to go?

To solve this issue we need to add validation to RNCAsyncStorage.m, which you can find here. Check if an array of keys is empty and then throw an error.

@jhoobergs would you like to fix it and send PR?

I think that we should unify the way in which we validate inputs for all methods. The API behavior should be consistent.

WDYT @krizzu ?

@jhoobergs
Copy link
Author

I have no experience with Objective C, so I can't really fix it.

@krizzu
Copy link
Member

krizzu commented Feb 13, 2019

I'm up for having feedback from actions - throwing an error tells us we could do something wrong. We can also make those Errors more meaningful, with hints of what might fix the issue (like "Check if the array is not empty" ).

@pbitkowski Feel free to post PR

@hawkrives
Copy link

Sure, but the API contract of "multiRemove" is "delete all of these things" - why special-case having zero items?

I don't particularly relish having to add application-level checks to make sure I'm not calling with an empty array; I often build up a list of keys from some other data, and if multiRemove throws on an empty array, I'd need to check before I call it.

That just seems like an odd design choice to me.

@krizzu
Copy link
Member

krizzu commented Feb 13, 2019

@hawkrives Thank you for the feedback, I understand your concern.

All of AsyncStorage methods are rejecting promise if something bad happens. That's why I'd suggest to wrap every AsyncStorage method call into try/catch, if using it in async function. Otherwise, callback is being called with error. It simply looks like empty array check is just missing from iOS.

@pbitkowski
Copy link
Contributor

@krizzu I'm going to prepare such PR soon.

@pbitkowski
Copy link
Contributor

pbitkowski commented Feb 14, 2019

I thought about it @krizzu, and the keys parameter in multiRemove is declared as Array<string>. An array can be empty. So maybe we should rather eliminate an error on Android side than create a new one on iOS side? What do you think?

https://github.com/react-native-community/react-native-async-storage/blob/65341b8d26ccbe8e9c21d2d1e7b340888668c72d/lib/AsyncStorage.js#L289

We've got the same situation with multiSet.

@krizzu
Copy link
Member

krizzu commented Feb 15, 2019

@pbitkowski @hawkrives @jhoobergs
I've come to realize that it would make more sense to have multiRemove not throwing an error on an empty array. The same principle can be applied to for multiSet.

If you'd like to help with it, PR that deletes array length check on android would be awesome.

Thanks for your input!

@krizzu
Copy link
Member

krizzu commented Mar 1, 2019

🎉 This issue has been resolved in version 1.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@krizzu krizzu added the released label Mar 1, 2019
tobyworks pushed a commit to my-channels/react-native-async-storage that referenced this issue Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted :octocat:
Projects
None yet
Development

No branches or pull requests

4 participants