Skip to content

Commit

Permalink
Async Tx collided with Logging's sync Tx. Undo the asynchronziation o…
Browse files Browse the repository at this point in the history
…f transactions #269

I tried to async everything all the way but it was a deep tumbling down the rabbit hole, so revert things back.
  • Loading branch information
MrCsabaToth committed Jul 25, 2023
1 parent 776e7cd commit 8c0ba08
Show file tree
Hide file tree
Showing 25 changed files with 112 additions and 112 deletions.
16 changes: 8 additions & 8 deletions lib/import/csv_importer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,8 @@ class CSVImporter with PowerSpeedMixin {
);

final extendTuning = prefService.get<bool>(extendTuningTag) ?? extendTuningDefault;
await database.writeTxn(() async {
await database.activitys.put(activity);
database.writeTxnSync(() {
database.activitys.putSync(activity);
});

final numRow = _lines.length - _linePointer;
Expand Down Expand Up @@ -599,8 +599,8 @@ class CSVImporter with PowerSpeedMixin {
heartRate: int.tryParse(values[2]),
sport: activity.sport,
);
await database.writeTxn(() async {
await database.records.put(record);
database.writeTxnSync(() {
database.records.putSync(record);
});

_linePointer++;
Expand Down Expand Up @@ -723,8 +723,8 @@ class CSVImporter with PowerSpeedMixin {
SchwinnACPerformancePlus.extraCalorieFactor;
}
energy += dEnergy;
await database.writeTxn(() async {
await database.records.put(record);
database.writeTxnSync(() {
database.records.putSync(record);
});

timeStamp = timeStamp.add(recordDuration);
Expand Down Expand Up @@ -753,8 +753,8 @@ class CSVImporter with PowerSpeedMixin {
activity.calories = energy.round();
}

await database.writeTxn(() async {
await database.activitys.put(activity);
database.writeTxnSync(() {
database.activitys.putSync(activity);
});

return activity;
Expand Down
16 changes: 8 additions & 8 deletions lib/persistence/isar/db_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class DbUtils {
return database.workoutSummarys.countSync() > 0;
}

Future<bool> hasRecords(Id activityId) async {
bool hasRecords(Id activityId) {
return activityId != Isar.minId &&
activityId != Isar.autoIncrement &&
await database.records.where().filter().activityIdEqualTo(activityId).count() > 0;
database.records.where().filter().activityIdEqualTo(activityId).countSync() > 0;
}

Future<List<Record>> getRecords(Id activityId) async {
Expand Down Expand Up @@ -61,8 +61,8 @@ class DbUtils {
// Speed already should have powerFactor effect
double dD = (record.speed ?? 0.0) * DeviceDescriptor.kmh2ms * dT;
record.distance = record.distance! + dD;
await database.writeTxn(() async {
await database.records.put(record);
database.writeTxnSync(() {
database.records.putSync(record);
});
}
}
Expand All @@ -72,8 +72,8 @@ class DbUtils {

if ((previousRecord.distance ?? 0.0) > eps && (activity.distance < eps || force)) {
activity.distance = previousRecord.distance!;
await database.writeTxn(() async {
await database.activitys.put(activity);
database.writeTxnSync(() {
database.activitys.putSync(activity);
});
}

Expand Down Expand Up @@ -186,8 +186,8 @@ class DbUtils {
}

if (updated > 0) {
await database.writeTxn(() async {
await database.activitys.put(activity);
database.writeTxnSync(() {
database.activitys.putSync(activity);
});
}

Expand Down
12 changes: 6 additions & 6 deletions lib/ui/activities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,9 @@ class ActivitiesScreenState extends State<ActivitiesScreen> with WidgetsBindingO
enableDrag: false,
);
if (sportPick != null) {
await _database.writeTxn(() async {
activity.sport = sportPick;
await _database.activitys.put(activity);
activity.sport = sportPick;
_database.writeTxnSync(() {
_database.activitys.putSync(activity);
});

setState(() {
Expand All @@ -311,9 +311,9 @@ class ActivitiesScreenState extends State<ActivitiesScreen> with WidgetsBindingO
confirm: TextButton(
child: const Text("Yes"),
onPressed: () async {
await _database.writeTxn(() async {
await _database.records.filter().activityIdEqualTo(activity.id).deleteAll();
await _database.activitys.delete(activity.id);
_database.writeTxnSync(() {
_database.records.filter().activityIdEqualTo(activity.id).deleteAllSync();
_database.activitys.deleteSync(activity.id);
setState(() {
_editCount++;
});
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/calorie_tunes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ class CalorieTunesScreenState extends State<CalorieTunesScreen> with WidgetsBind
middleText: 'Are you sure to delete this Tune?',
confirm: TextButton(
child: const Text("Yes"),
onPressed: () async {
await _database.writeTxn(() async {
await _database.calorieTunes.delete(calorieTune.id);
onPressed: () {
_database.writeTxnSync(() {
_database.calorieTunes.deleteSync(calorieTune.id);
setState(() {
_editCount++;
});
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/device_usages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class DeviceUsagesScreenState extends State<DeviceUsagesScreen> with WidgetsBind
if (sportPick != null) {
deviceUsage.sport = sportPick;
deviceUsage.time = DateTime.now();
await _database.writeTxn(() async {
await _database.deviceUsages.put(deviceUsage);
_database.writeTxnSync(() {
_database.deviceUsages.putSync(deviceUsage);
setState(() {
_editCount++;
});
Expand All @@ -99,9 +99,9 @@ class DeviceUsagesScreenState extends State<DeviceUsagesScreen> with WidgetsBind
middleText: 'Are you sure to delete this Usage?',
confirm: TextButton(
child: const Text("Yes"),
onPressed: () async {
await _database.writeTxn(() async {
await _database.deviceUsages.delete(deviceUsage.id);
onPressed: () {
_database.writeTxnSync(() {
_database.deviceUsages.deleteSync(deviceUsage.id);
setState(() {
_editCount++;
});
Expand Down
20 changes: 10 additions & 10 deletions lib/ui/find_devices.dart
Original file line number Diff line number Diff line change
Expand Up @@ -631,8 +631,8 @@ class FindDevicesState extends State<FindDevicesScreen> {
manufacturer: advertisementDigest.manufacturer,
time: DateTime.now(),
);
await database.writeTxn(() async {
await database.deviceUsages.put(deviceUsage!);
database.writeTxnSync(() {
database.deviceUsages.putSync(deviceUsage!);
});
}
}
Expand Down Expand Up @@ -678,8 +678,8 @@ class FindDevicesState extends State<FindDevicesScreen> {
if (deviceUsage != null) {
deviceUsage.sport = sportPick;
deviceUsage.time = DateTime.now();
await database.writeTxn(() async {
await database.deviceUsages.put(deviceUsage!);
database.writeTxnSync(() {
database.deviceUsages.putSync(deviceUsage!);
});
} else {
deviceUsage = DeviceUsage(
Expand All @@ -689,14 +689,14 @@ class FindDevicesState extends State<FindDevicesScreen> {
manufacturer: advertisementDigest.manufacturer,
time: DateTime.now(),
);
await database.writeTxn(() async {
await database.deviceUsages.put(deviceUsage!);
database.writeTxnSync(() {
database.deviceUsages.putSync(deviceUsage!);
});
}
} else {
descriptor.sport = deviceUsage.sport;
await database.writeTxn(() async {
await database.deviceUsages.put(deviceUsage!);
database.writeTxnSync(() {
database.deviceUsages.putSync(deviceUsage!);
});
}
}
Expand Down Expand Up @@ -760,8 +760,8 @@ class FindDevicesState extends State<FindDevicesScreen> {
if (deviceUsage != null) {
deviceUsage.manufacturerName = fitnessEquipment.manufacturerName;
deviceUsage.time = DateTime.now();
await database.writeTxn(() async {
await database.deviceUsages.put(deviceUsage!);
database.writeTxnSync(() {
database.deviceUsages.putSync(deviceUsage!);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/ui/import_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class ImportFormState extends State<ImportForm> {
final workoutSummary = activity
.getWorkoutSummary(deviceDescriptor.manufacturerNamePart);
final database = Get.find<Isar>();
await database.writeTxn(() async {
await database.workoutSummarys.put(workoutSummary);
database.writeTxnSync(() {
database.workoutSummarys.putSync(workoutSummary);
});
}
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/leaderboards/device_leaderboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class DeviceLeaderboardScreenState extends State<DeviceLeaderboardScreen>
middleText: 'Are you sure to delete this entry?',
confirm: TextButton(
child: const Text("Yes"),
onPressed: () async {
await _database.writeTxn(() async {
await _database.workoutSummarys.delete(workoutSummary.id);
onPressed: () {
_database.writeTxnSync(() {
_database.workoutSummarys.deleteSync(workoutSummary.id);
setState(() {
_editCount++;
});
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/leaderboards/sport_leaderboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class SportLeaderboardScreenState extends State<SportLeaderboardScreen>
middleText: 'Are you sure to delete this entry?',
confirm: TextButton(
child: const Text("Yes"),
onPressed: () async {
await _database.writeTxn(() async {
await _database.workoutSummarys.delete(workoutSummary.id);
onPressed: () {
_database.writeTxnSync(() {
_database.workoutSummarys.deleteSync(workoutSummary.id);
setState(() {
_editCount++;
});
Expand Down
8 changes: 4 additions & 4 deletions lib/ui/parts/calorie_factor_tune.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class CalorieFactorTuneBottomSheetState extends State<CalorieFactorTuneBottomShe
.findFirst();
if (calorieTune != null) {
calorieTune.calorieFactor = calorieFactor;
await database.writeTxn(() async {
await database.calorieTunes.put(calorieTune);
database.writeTxnSync(() {
database.calorieTunes.putSync(calorieTune);
});
} else {
final calorieTune = CalorieTune(
Expand All @@ -83,8 +83,8 @@ class CalorieFactorTuneBottomSheetState extends State<CalorieFactorTuneBottomShe
hrBased: widget.hrBased,
time: DateTime.now(),
);
await database.writeTxn(() async {
await database.calorieTunes.put(calorieTune);
database.writeTxnSync(() {
database.calorieTunes.putSync(calorieTune);
});
}

Expand Down
8 changes: 4 additions & 4 deletions lib/ui/parts/calorie_override.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ class CalorieOverrideBottomSheetState extends State<CalorieOverrideBottomSheet>
.findFirst();
if (calorieTune != null) {
calorieTune.calorieFactor = calorieFactor;
await database.writeTxn(() async {
await database.calorieTunes.put(calorieTune);
database.writeTxnSync(() {
database.calorieTunes.putSync(calorieTune);
});
} else {
final calorieTune = CalorieTune(
Expand All @@ -96,8 +96,8 @@ class CalorieOverrideBottomSheetState extends State<CalorieOverrideBottomSheet>
hrBased: widget.hrBased,
time: DateTime.now(),
);
await database.writeTxn(() async {
await database.calorieTunes.put(calorieTune);
database.writeTxnSync(() {
database.calorieTunes.putSync(calorieTune);
});
}
Get.back(result: calorieFactor);
Expand Down
36 changes: 18 additions & 18 deletions lib/ui/parts/database_migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,10 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
}

if (activity.id == Isar.autoIncrement) {
await database.writeTxn(() async {
await database.activitys.put(activity);
database.writeTxnSync(() {
database.activitys.putSync(activity);
assert(activity.id != Isar.autoIncrement);
await database.floorMigrations.put(FloorMigration(
database.floorMigrations.putSync(FloorMigration(
entityName: "Activity",
floorId: floorActivity.id!,
isarId: activity.id,
Expand Down Expand Up @@ -166,16 +166,16 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
))
.toList(growable: false);

await database.writeTxn(() async {
database.records.putAll(records);
database.writeTxnSync(() {
database.records.putAllSync(records);
final recordMigrations = IterableZip([
floorRecords.map((fr) => fr.id),
records.map((ir) => ir.id),
])
.map((pair) => FloorRecordMigration(
activityId: activity.id, floorId: pair[0]!, isarId: pair[1]!))
.toList(growable: false);
await database.floorRecordMigrations.putAll(recordMigrations);
database.floorRecordMigrations.putAllSync(recordMigrations);
});

migrationItemCounter++;
Expand Down Expand Up @@ -207,16 +207,16 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
))
.toList(growable: false);

await database.writeTxn(() async {
await database.deviceUsages.putAll(deviceUsages);
database.writeTxnSync(() {
database.deviceUsages.putAllSync(deviceUsages);
final deviceUsageMigrations = IterableZip([
floorDeviceUsages.map((fdu) => fdu.id),
deviceUsages.map((idu) => idu.id),
])
.map((pair) =>
FloorMigration(entityName: "DeviceUsage", floorId: pair[0]!, isarId: pair[1]!))
.toList(growable: false);
await database.floorMigrations.putAll(deviceUsageMigrations);
database.floorMigrations.putAllSync(deviceUsageMigrations);
});

migrationItemCounter++;
Expand Down Expand Up @@ -244,16 +244,16 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
))
.toList(growable: false);

await database.writeTxn(() async {
await database.calorieTunes.putAll(calorieTunes);
database.writeTxnSync(() {
database.calorieTunes.putAllSync(calorieTunes);
final calorieTuneMigrations = IterableZip([
floorCalorieTunes.map((fct) => fct.id),
calorieTunes.map((ict) => ict.id),
])
.map((pair) =>
FloorMigration(entityName: "CalorieTune", floorId: pair[0]!, isarId: pair[1]!))
.toList(growable: false);
await database.floorMigrations.putAll(calorieTuneMigrations);
database.floorMigrations.putAllSync(calorieTuneMigrations);
});

migrationItemCounter++;
Expand All @@ -280,16 +280,16 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
))
.toList(growable: false);

await database.writeTxn(() async {
await database.powerTunes.putAll(powerTunes);
database.writeTxnSync(() {
database.powerTunes.putAllSync(powerTunes);
final powerTuneMigrations = IterableZip([
floorPowerTunes.map((fct) => fct.id),
powerTunes.map((ict) => ict.id),
])
.map((pair) =>
FloorMigration(entityName: "PowerTune", floorId: pair[0]!, isarId: pair[1]!))
.toList(growable: false);
await database.floorMigrations.putAll(powerTuneMigrations);
database.floorMigrations.putAllSync(powerTuneMigrations);
});

migrationItemCounter++;
Expand Down Expand Up @@ -323,16 +323,16 @@ class DatabaseMigrationBottomSheetState extends State<DatabaseMigrationBottomShe
))
.toList(growable: false);

await database.writeTxn(() async {
await database.workoutSummarys.putAll(workoutSummaries);
database.writeTxnSync(() {
database.workoutSummarys.putAllSync(workoutSummaries);
final workoutSummaryMigrations = IterableZip([
floorWorkoutSummaries.map((fws) => fws.id),
workoutSummaries.map((iws) => iws.id),
])
.map((pair) =>
FloorMigration(entityName: "WorkoutSummary", floorId: pair[0]!, isarId: pair[1]!))
.toList(growable: false);
await database.floorMigrations.putAll(workoutSummaryMigrations);
database.floorMigrations.putAllSync(workoutSummaryMigrations);
});

migrationItemCounter++;
Expand Down

0 comments on commit 8c0ba08

Please sign in to comment.