isar / hive

Lightweight and blazing fast key-value database written in pure Dart.
Apache License 2.0
4.02k stars 401 forks source link

Does hive support adapters for external source files ? #373

Closed cgonzalezandrades closed 4 years ago

cgonzalezandrades commented 4 years ago

Question Please explain the problem you are running into. I need an adapter for GeoPoint and I dont see any way to create an adapter for an external source file in Hive docs. is this even supported ?

Code sample

Provide a few simple lines of code to show your problem.

Version

MarcelGarus commented 4 years ago

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

themisir commented 4 years ago

You can check create adapter manually section from hive docs.

cgonzalezandrades commented 4 years ago

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

Thank you @marcelgarus What about DocumentSnapshot ? I've been at it for days now and I havent have any luck. I read the documentation for creating an adapter manually but I cant figure out how to do it for DocumentSnapshot

MarcelGarus commented 4 years ago

I just took a look at DocumentSnapshot and there's no way to serialize that using Hive – or any other serializer for that matter. The reason is that it's not a pure data-like class, but a complete interactive object – for example, it contains a reference to the current FirebaseFirestore instance, which you simply can't save to Hive because it represents an active Firestore connection. Also, the constructor is private, so it can only be called by Firestore itself.

You should rather save the actual data itself to Hive instead of the snapshot containing the data. For example, if you have a DocumentSnapshot<User>, you should save the User to Hive.

cgonzalezandrades commented 4 years ago

That's what I thought so implemented a solution in my code of not saving the DocumentSnapshot data to Hive.

Thank you @marcelgarus

mllrr96 commented 2 years ago

Not sure if it's officially endorsed, but it's certainly possible by writing your own TypeAdapter:

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 100; // something

  @override
  void write(BinaryWriter writer, GeoPoint point) {
    writer
      ..write(point.x)
      ..write(poin.y);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as int,
      reader.read() as int,
    );
  }
}

Disclaimer: Code written from memory, not tested. Actual syntax may differ.

I'm using Type Adapter for Geopoint, your code helped me understand how it works, I noticed couple of things were incorrect so I'm including my code here (tested and it's working), again thx for your help :)

Also for folks reading this don't forget to register the adapter

Hive.registerAdapter(GeoPointAdapter());

class GeoPointAdapter extends TypeAdapter<GeoPoint> {
  @override
  int get typeId => 32; // add unique int here

  @override
  void write(BinaryWriter writer, GeoPoint obj) {
    writer
      ..write(obj.latitude)
      ..write(obj.longitude);
  }

  @override
  GeoPoint read(BinaryReader reader) {
    return GeoPoint(
      reader.read() as double,
      reader.read() as double,
    );
  }
}