Closed VadimDubovoy closed 1 year ago
It seems SortDirection.DESCENDING is currently broken in observeQuery? Experiencing this. SortDirection.ASCENDING works just fine.
It seems SortDirection.DESCENDING is currently broken in observeQuery? Experiencing this. SortDirection.ASCENDING works just fine.
Sorting works fine in my case. The last renamed item goes first despite the fact I receive outdated data (updatedAt incl.) from observeQuery.
Thanks for opening the issue @VadimDubovoy. I was able to repro following the steps you provided. Labeling this as a bug. The team will work on patching it.
As @e-simpson mentioned above, the stale results are only emitted when specifying SortDirection.DESCENDING
, so you can use one of the following temporary workarounds in the meantime:
Omit the sort
param and manually sort the results in the observeQuery
callback before applying them to your UI state
const sub = DataStore.observeQuery(Post).subscribe(({ items }) => {
setList(
items.sort((a, b) =>
a.updatedAt < b.updatedAt ? 1 : a.updatedAt > b.updatedAt ? -1 : 0
)
);
});
OR
Use observe
or observeQuery
as a trigger to perform a sorted query
useEffect(() => {
const sub = DataStore.observe(Post).subscribe(() => getPosts());
}, []);
async function getPosts() { const results = await DataStore.query(Post, Predicates.ALL, { sort: (s) => s.updatedAt(SortDirection.DESCENDING), }); setList(results); }
Thanks for reply @iartemiev. Turns out I didn't understand @e-simpson 's comment. The workarounds work as expected. Hope to see the bug fixed soon.
We released a fix for this issue in aws-amplify@5.0.22
Before opening, please confirm:
JavaScript Framework
React
Amplify APIs
DataStore
Amplify Categories
Not applicable
Environment information
Describe the bug
I have a list of items queried by observeQuery and stored in state. On the same page I have an input to rename one of the items. Every time I rename an item, it gives me a new array of items, but this items have new names only after the first change. Any following changes won't be reflected in the items I got from observer, unless I resubscribe.
Expected behavior
Receive fresh fully updated items after any number of changes with no need to unsubscribe/subscribe.
Reproduction steps
Code Snippet
Setup observeQuery:
Update a name:
Try to change name at least two times.
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response