flutterdata / flutter_data

Seamlessly manage persistent data in your Flutter apps
MIT License
410 stars 31 forks source link

Relationships lost when app is restarted. #218

Closed PedroSteinmacherEngelhardt closed 1 year ago

PedroSteinmacherEngelhardt commented 1 year ago

Hello, I have noticed an issue with my app where, after creating a new object with the Deposito class offline and then restarting the app, all references to relationships are lost.

@JsonSerializable()
@DataRepository([
  BaseAdapter,
  SafeDeserialize,
  DepositoAdapter
])
class Deposito extends DataModel<Deposito> {
  @override
  final String? id;
  final String descricao;
  final String tratado;
  final String eliminado;
  final String lon;
  final String lat;
  final String system_user_id;
  final String created_at;
  final String? updated_at;

  @JsonKey(name: "atividade_id")
  final BelongsTo<Atividade> atividade;
  @JsonKey(name: "deposito_tipo_id")
  final BelongsTo<DepositoTipo> deposito_tipo;

  final BelongsTo<Amostra> amostra;
  final BelongsTo<Tratamento> tratamento;

  Deposito({
    this.id,
    required this.descricao,
    this.tratado = "N",
    this.eliminado = "N",
    required this.lon,
    required this.lat,
    required this.system_user_id,
    required this.created_at,
    required this.updated_at,
    required this.atividade,
    required this.deposito_tipo,
    required this.amostra,
    required this.tratamento,
  });

  void MarcarEliminado() async {
    await this.copyWith(eliminado: eliminado == "S" ? "N" : "S").withKeyOf(this).save();
    log(eliminado.toString());
  }

  static Future<void> create(Ref ref, String descricao, DepositoTipo deposito_tipo, Atividade atividade) async {
    final SessionModel session = ref.read(sessionProvider)!;
    final Position position;
    try {
      position = await Geolocator.getCurrentPosition().timeout(Duration(seconds: 5));
    } catch (e) {return;}

    final Deposito deposito = Deposito(
      atividade: atividade.asBelongsTo,
      descricao: descricao,
      lon: position.longitude.toString(),
      lat: position.latitude.toString(),
      system_user_id: session.id.toString(),
      created_at: DateTime.now().toCustomDateTime(),
      deposito_tipo: deposito_tipo.asBelongsTo,
      tratamento: BelongsTo(),
      amostra: BelongsTo(),
      updated_at: null,
    );

    deposito.save();
  }
}
frank06 commented 1 year ago

Only when saving it offline?

I will have a look

PedroSteinmacherEngelhardt commented 1 year ago

Yes, everthing seem to be working just fine when saving it online.

frank06 commented 1 year ago

@PedroSteinmacherEngelhardt

You create the model offline and then restart while still being offline, or are you back online when you refresh?

If it's the latter, then I think I know what's going on:

Did you configure your app to retry offline operations?

I'm currently testing the case where you create a model offline + restart the app while offline and things work just fine.

frank06 commented 1 year ago

Check out 1.5.9 and let me know what I asked you. If you keep having issues feel free to reopen.