Naografix
Naografix

Reputation: 959

Isar & Flutter: Updating a large object

I'm working with the Isar database 4.0.0-dev.14 in Flutter, and I'm trying to save a large object called Target. The problem is I can have a large list of "pings", like 30-40k pings at the end. The goal of my app is to "ping" some devices every 100ms and, every 5 seconds, to save them into my isar db.

Here is the structure of my Target and Ping classes:

import 'package:isar/isar.dart';

part 'target_dto.g.dart';

@collection
class TargetDto {
  final int id;
  final List<PingDto> pings;

  TargetDto({
    required this.id,
    required this.pings,
  });

  // Convert TargetDto to Target model
  Target toModel() {
    return Target(
      idDatabase: id,
      pings: pings.map((e) => e.toModel()).toList(),
    );
  }
}

@collection
class PingDto {
  final int id;
  final String name;

  PingDto({
    required this.id,
    required this.name,
  });

  // Convert PingDto to Ping model
  Ping toModel() {
    return Ping(
      id: id,
      name: name,
    );
  }
}

class Target {
  int idDatabase;
  List<Ping> pings;

  Target({
    required this.idDatabase,
    required this.pings,
  });

  // Convert Target model to TargetDto
  TargetDto toDto() {
    return TargetDto(
      id: idDatabase,
      pings: pings.map((ping) => ping.toDto()).toList(),
    );
  }
}

class Ping {
  int id;
  String name;

  Ping({
    required this.id,
    required this.name,
  });

  // Convert Ping model to PingDto
  PingDto toDto() {
    return PingDto(
      id: id,
      name: name,
    );
  }
}

I am trying to save the Target object using the following code:

void update(Target target) {
    isar.write((isar) => isar.targetDtos.put(target.toDto(target.idDatabase)));
 }

However, I am not sure if this is the correct way to save an object with a nested list of Ping objects... Currently it takes like 1.3s to save it...

I tried something like this, but it's worse:

void update(Target target) {
  final targetDto = target.toDto(target.idDatabase); 
  final targetFromDb = isar.targetDtos.get(target.idDatabase);

  if (targetFromDb != null) {
    targetFromDb.pings = targetDto.pings;

    isar.write((isar) => isar.targetDtos.put(targetFromDb));
  } 
}  

Any guidance would be appreciated!

Upvotes: 0

Views: 53

Answers (0)

Related Questions