Skip to content

Commit

Permalink
Throw exception when Hive.init() is called more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
simc committed Dec 14, 2020
1 parent c2e31ea commit 59ad540
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions hive/lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {

@override
void init(String path) {
if (homePath != null) {

This comment has been minimized.

Copy link
@felangel

felangel Jan 27, 2021

@leisim why was this change made? Previously it was possible to have multiple instances as described in #336 (comment). This change makes that use case no longer possible.

This comment has been minimized.

Copy link
@themisir

themisir Feb 15, 2021

Contributor

This was added to fix another bug caused when initializing same Hive instance multiple times. So Simon decided to deny multiple initializations.

This comment has been minimized.

Copy link
@simc

simc Feb 15, 2021

Author Member

I didn't think about use cases where it might be needed. Any ideas how we can make sure that users don't misuse this method and still allow multiple instances?

This comment has been minimized.

Copy link
@tiloc

tiloc Feb 15, 2021

Could you make it idempotent?
I.e. if it is invoked for a new path for the first time, initialise for real and return the result or throw on failure.
if it is invoked again after successful initial init for the same path, then just do nothing and return success.

throw HiveError('Instance has already been initialized.');
}
homePath = path;

_boxes.clear();
}

Future<BoxBase<E>> _openBox<E>(
Expand Down

1 comment on commit 59ad540

@tiloc
Copy link

@tiloc tiloc commented on 59ad540 Feb 15, 2021

Choose a reason for hiding this comment

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

Apart from the remark by felangel, this is also removing idempotency from this API call. Ideally it should just silently do nothing and report success.

Please sign in to comment.