Flutter应用程序的数据存储与DynamoDB不同步。

huangapple go评论93阅读模式
英文:

Flutter app datastore not syncing with DynamoDB

问题

我的Flutter应用无法检索DynamoDB中新增的项目。

我在DynamoDB中有一个项目(id: abc012345)。我的Flutter应用允许我通过id显示项目。最初,我搜索了项目(id: abc012345)并在我的应用中显示出来。当我使用AppSync的Lambda函数从DynamoDB中删除它(ttl为0,所以它立即消失),该项目被删除并在我的应用中消失,这是预期的行为。然而,当我在DynamoDB中使用相同的id(abc012345)创建一个新项目并再次进行相同的过程时,我的应用无法找到该项目,而使用不同id添加项目则没有问题。我需要手动清除手机内部存储以使其重新同步。

我猜测重复使用id会导致缓存数据与后端实际数据之间的不一致。如果我确实需要重复使用id,我该怎么办?

英文:

My flutter app cannot retrieve the newly added item in the DynamoDB.

I have an item(id: abc012345) in the dynamodb. My flutter app allows me to display the item by their id. Initially, I searched the item(id: abc012345) and displayed on my app. When I removed it from the dynamodb with a lambda function using appsync (ttl is 0, so it's gone immediately), the item is removed and disappeared on my app, which is an expected behaviour. However, when I create a new item with the same id(abc012345) in the dynamodb and go through the same process again, my app cannot find the item while adding an item with a different id is fine. I need to manually clear the phone internal storage to make it sync again.

I guess reusing the id causes inconsistencies between the cached data and the actual data from the backend. What can I do if I really have to reuse the id?

答案1

得分: 0

我找到了一个解决方法。每当从Dynamodb中删除一个项目时,我会清除数据存储。

void observeDevice() {
  final devicesStream = _deviceRepo.observeDevices();
  devicesStream.listen((event) async {
    if (event.eventType == EventType.delete) {
      await Amplify.DataStore.clear();
      await Future.delayed(const Duration(seconds: 2));
    }
    getDevices();
  });
}

请注意,这是一个代码片段,用于观察设备并在删除事件发生时清除数据存储。

英文:

Found a workaround to this. I clear the datastore everytime an item is deleted from Dynamodb.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

  void observeDevice() {
    final devicesStream = _deviceRepo.observeDevices();
    devicesStream.listen((event) async {
      if (event.eventType == EventType.delete) {
        await Amplify.DataStore.clear();
        await Future.delayed(const Duration(seconds: 2));
      }
      getDevices();
    });
  }

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月8日 22:16:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定