Skip to content

DataStore.observe() can miss events if other DataStore methods are called immediately before it #1590

@Jordan-Nelson

Description

@Jordan-Nelson

Description

Amplify.DataStore.observe() can miss events if other DataStore methods are called immediately before it. for example, if DataStore.save() is called immediately before DataStore.observe(), the observe stream will miss the save event.

Here is an example test case that will fail.

testWidgets('should emit an event for each item saved',
    (WidgetTester tester) async {
  var itemStream = Amplify.DataStore.observe(Blog.classType)
      .map((event) => event.item.name);

  expectLater(
    itemStream,
    emitsInOrder(['blog 1', 'blog 2', 'blog 3']),
  );

  // The test will pass with the delay below added. Without the delay,
  // the first event will not be emitted from the stream.

  // await Future.delayed(Duration(seconds: 2));

  await Amplify.DataStore.save(Blog(name: 'blog 1'));
  await Amplify.DataStore.save(Blog(name: 'blog 2'));
  await Amplify.DataStore.save(Blog(name: 'blog 3'));
});

Since observeQuery is built on top of observe, this impact observeQuery as well.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Storage

Steps to Reproduce

See example in description.

Screenshots

No response

Platforms

  • iOS
  • Android

Android Device/Emulator API Level

No response

Environment

N/A - Multiple flutter versions (2.8.1 through 3.0.0)

Dependencies

N/A

Device

iPhone 12 (seems to be present on Android as well)

OS

iOS 15.1 (seems to be present on Android as well)

CLI Version

N/A

Additional Context

This seems to be causes by a race condition in DataStore. All datastore methods (save/delete) are supposed to wait for the observe stream to be set up prior to executing. This does not seem to be happening. This race condition may be slightly worse in flutter 3, although more testing needs to be done to confirm this.

Metadata

Metadata

Assignees

Labels

bugSomething is not working; the issue has reproducible steps and has been reproduceddatastoreIssues related to the DataStore CategoryiOSIssues specific to the iOS Platform

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions