Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing remote relationship after offline retry #267

Open
alexrothm opened this issue Apr 15, 2024 · 4 comments
Open

Missing remote relationship after offline retry #267

alexrothm opened this issue Apr 15, 2024 · 4 comments

Comments

@alexrothm
Copy link

Hi, when creating objects with relationships directly one after the other, or in offline mode, the remote server does not have the relationship after posting data via the retry offlineOperation.

I have two models. Crop and Cultivar. Crops can have multiple Cultivars and Cultivars may be assigned to a Crop.

@JsonSerializable()
@DataRepository([JsonServerAdapter])
class Crop extends DataModel<Crop> {
  @override
  final int? id;
  final String name;
  final HasMany<Cultivar>? cultivars;

  Crop({this.id, required this.name, HasMany<Cultivar>? cultivars})
      : cultivars = cultivars ?? HasMany();

  int getCultivarsCount() {
    return cultivars!.length > 0 ? cultivars!.length : 0;
  }
}
@JsonSerializable(fieldRename: FieldRename.snake)
@DataRepository([JsonServerAdapter])
class Cultivar extends DataModel<Cultivar> {
  @override
  final int? id;
  final String name;
  final String ripeningTime;
  late final BelongsTo<Crop> crop;
  final String cropName;

  Cultivar(
      {this.id,
      required this.name,
      required this.ripeningTime,
      BelongsTo<Crop>? crop,
      required this.cropName})
      : crop = crop ?? BelongsTo();
}

When I do the following only the local repository has the relationship saved. The remote one does not save any id for the corresponding crop:

OutlinedButton(
    onPressed: () {
      Crop newCrop = Crop(name: 'MyCrop');
      newCrop.save();
    
      Cultivar newCultivar = Cultivar(name: 'MyCultivar',
          ripeningTime: Cultivar.ripeningTimeEarly, crop: BelongsTo(newCrop), cropName: newCrop.name);
      newCultivar.save();
    }...),

When a change the function to async and add await before each save the relation is stored correctly.

But, when I shut down the remote server and the offline functionality retries both posts. The crop id is empty for the newCultivar.

Summary:
The desiged objects should look like this:

Crop: {"id":15,"name":"MyCrop"}
Cultivar: {"id":15, "name":"MyCultivar","ripening_time":"RIPENING_TIME_EARLY","crop":15,"crop_name":"MyCrop"}

but in offline mode or without using async + await I get this instead:

Crop: {"id":15,"name":"MyCrop"}
Cultivar: {"id":15, "name":"MyCultivar","ripening_time":"RIPENING_TIME_EARLY","crop":null,"crop_name":"MyCrop"}

I appreciate any help.

@frank06
Copy link
Contributor

frank06 commented Apr 24, 2024

Makes sense as retries will attempt to send the exact same original request. Changes after that are not captured. I'm not sure, maybe we should re-serialize the updated model

@frank06
Copy link
Contributor

frank06 commented Sep 19, 2024

Need to decide whether to re-serialize the updated model

@drexhacker
Copy link

@frank06 Thanks for the Good work again on this great package.
I am currently facing the same exact issue where by when we save offline we loose relationships when we reconnect to internet.
Am currently using version 1.6.0 of the package and everything is working well except this.

When I was starting my project I was stack at which backend to use either Custom REST API (FastAPI) or firebase but then came across appwrite which worked well but no offline support. So after doing some research and considering I have been using this package flutter_data in my REST backed projects. I came up with a solution to interpret flutter_data's REST calls to appwrite SDK calls which in turn resulted in a working appwrite + flutter_data intergration via a custom adapter that has support for CRUD operations, Realtime subscription, Permissions, Querying, & Relationships in a simple to use flutter package appwrite_offline.

So Kindly I request you link it among compatible/supported backends of flutter_data, if you see fit and also feel free to contribute to this package as well if you find time or even just advice or suggest and I will gladly appreciate.

@drexhacker
Copy link

@alexrothm @frank06 I think this issue traces back to the fact that the offline version of the saved model doesn't have an id until assigned by the remote adapter when back online hence causing relationship issues since they [in my thinking] solely depend on model ids. I managed to notice this when I used this package in a completely offline app, everything worked fine initially but after a few navigations or restarts the relationships where lost and when I assigned auto ids to them they persisted even after restarts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants