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

Avoid duplicating objects. #842

Open
1 task
Turskyi opened this issue Nov 25, 2021 · 1 comment
Open
1 task

Avoid duplicating objects. #842

Turskyi opened this issue Nov 25, 2021 · 1 comment
Labels
question Further information is requested

Comments

@Turskyi
Copy link

Turskyi commented Nov 25, 2021

Question
How to avoid duplicating objects in datastore?
What is the best practice let Hive know that the object must be overridden or ignored instead of duplicating.

Code sample
Future addCountries(List countries) async {
final Box box = await openDataBox();
box.addAll(countries);
}

Future<List> getCountries() async {
final List countries = [];
final Box box = await openDataBox();
countries.addAll(box.values.toList());
return countries;
}

Version

  • Platform: iOS, Android, Web
  • Flutter version: [ 2.5.3]
  • Hive version: [2.0.4]

Tasks

@Turskyi Turskyi added the question Further information is requested label Nov 25, 2021
@keezysilencer
Copy link

keezysilencer commented Dec 16, 2021

If you are retrieving new data from your API you can clear your box and add the new ones.

Future addCountries(List countries) async {
final box = await openDataBox();
await box.clear()
await box.addAll(countries);
}

If the data is not always new but you want to keep track of old data do:
Future addCountries(List countries) async {
final box = await openDataBox();
final existing = await getCountries(); // This returns the data you have already stored
final result = existing.addAll(countries).toSet(); // This removes duplicates
await box.addAll(result);
}

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

No branches or pull requests

2 participants