hive icon indicating copy to clipboard operation
hive copied to clipboard

Why does save() not persist to disk if you terminate the program abnormally after the save function is called?

Open MidnightStuff opened this issue 3 years ago • 0 comments

Question Why doesn't HIVE save data to disk on save()? For instance, if I use the save() function on a BOX, but then I terminate the process without properly closing the app, the data doesn't persist; however, if I close the app properly, the data gets saved to disk. I'm new to Dart and Hive, so maybe I just don't understand properly how to use it. I want the data to persist on disk, even if the app ends from a crash, but I can't seem to get it right.

I'm using Dart without Flutter for a console app on Windows, so I couldn't get the Hive Generator to work, or I just don't know how to do use it, so I tried to create a custom adapter. Maybe I'm missing something that would make it work?

In the example code, medForm tries to add a medication to the user and save it, but if I terminate the process after saving it, the medication doesn't persist on loading the app again. Does the save function not work like that? I'm missing something simple, probably.

I thought, maybe it doesn't save because the hash codes are the same, so I added an override to my User class, but that didn't help. Unsure what I'm missing. Is it because the List is changed within the User class? Does it not reflect save changes to modifications to the List within the HiveObject?

Code sample

import 'package:hive/hive.dart';
import 'Medication.dart';
import 'package:collection/collection.dart';

class User extends HiveObject {
  String name = "Anthony";

  List<Medication> meds;

  User(this.name, this.meds);

  @override
  int get hashCode => DeepCollectionEquality().hashCode;

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          const DeepCollectionEquality().equals(this, other) &&
          meds.length == other.meds.length;
}

class UserAdapter extends TypeAdapter<User> {
  @override
  final typeId = 0;

  @override
  User read(BinaryReader reader) {
    return User(reader.readString(), reader.readList().cast<Medication>());
  }

  @override
  void write(BinaryWriter writer, User obj) {
    writer.writeString(obj.name);
    writer.writeList(obj.meds);
  }
}

  medForm() {
    MedManager medManager;
    Medication medToAdd;

    print('''
Name: 
''');
    String name = stdin.readLineSync()!;

    int? rxNumber;

    while (true) {
      print('''
rxNumber: 
''');
      rxNumber = int.tryParse(stdin.readLineSync()!);
      if (rxNumber != null) {
        break;
      } else {
        print('Numbers only. Try again');
      }
    }

    print('''
usage: 
''');
    String usage = stdin.readLineSync()!;

    print('''
dosage: 
''');
    String dosage = stdin.readLineSync()!;

    int? count;
    while (true) {
      print('''
count: 
''');
      count = int.tryParse(stdin.readLineSync()!);
      if (count != null) {
        break;
      } else {
        print('Numbers only. Try again');
      }
    }

    print('''
refillDate: 
''');
    String refillDate = stdin.readLineSync()!;

    int? daysUntilRefill;
    while (true) {
      print('''
daysUntilRefill: 
''');
      daysUntilRefill = int.tryParse(stdin.readLineSync()!);
      if (daysUntilRefill != null) {
        break;
      } else {
        print('Numbers only. Try again');
      }
    }

    int? refillsRemaining;
    while (true) {
      print('''
refillsRemaining: 
''');
      refillsRemaining = int.tryParse(stdin.readLineSync()!);
      if (refillsRemaining != null) {
        break;
      } else {
        print('Numbers only. Try again');
      }
    }

    medToAdd = Medication(name, rxNumber, usage, dosage, count, refillDate,
        daysUntilRefill, refillsRemaining);
    selectedUser.meds.add(medToAdd);

    selectedUser.save();
  }

Version

  • Platform: Windows
  • Dart version: [>=2.17.6 <3.0.0]
  • Hive version: [2.2.3]

MidnightStuff avatar Aug 28 '22 11:08 MidnightStuff