Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: return properly sorted messages from offline DB #2996

Merged
merged 1 commit into from
Mar 6, 2025

Conversation

isekovanic
Copy link
Contributor

@isekovanic isekovanic commented Mar 6, 2025

🎯 Goal

This PR fixes a regression that we introduced about a month ago which causes a specific chain of events to break pagination in the MessageList whenever offline support is enabled.

Steps to reproduce:

  • Open a freshly installed app
  • Load the ChannelList component
  • Close the app or log out (this is important, so that we don't receive any WS events)
  • Receive some messages in a channel (anything >= 1 message)
  • Open the app again
  • Open the channel which received messages
  • You are now unable to paginate to previous messages

With a recent bugfix to pagination, we pessimistically set the pagination cursors to false as an initial value. The bugfix relies on the fact that whenever we query for new messages, we should heuristically be able to figure out whether we have earlier (or later) messages to load through the sorting of the messages. However, when offline support is enabled we do not in fact do a query first as we optimistically fetch whatever's in the DB and only then query. This causes the pagination state to be irreparably broken and unable to recover (even with closing and reopening the app).

The culprit was the fact that we were not properly sorting messages from our local SQLite instance and they arrived in reverse order, breaking the heuristics. After the messages are consumed, sorting is still applied so it was not noticeable at all. This "issue" has been dormant for a very long time, however due to the fact that we always had the pagination cursors set to true initially the issue has not surfaced until now.

The reason why the sorting is wrong is because SQLite has a hard time parsing ISOString instances of a date. This means that it will lose precision and sometimes even fallback to alternative sorting if it cannot figure the input out. In our case, it caused the order of the messages to be reversed as it falls back to sorting lexicographically (and ISOStrings can be sorted like so) causing the bug.

To fix this, we resort to converting the date into a UNIX timestamp and sorting that way, as it's:

  • Much more precise
  • Much quicker

Linear ticket: https://linear.app/stream/issue/REACT-306/pagination-sometimes-breaks-when-offline-support-is-enabled

Possibly also fixes this GH issue: #2994

🛠 Implementation details

🎨 UI Changes

iOS
Before After
Android
Before After

🧪 Testing

☑️ Checklist

  • I have signed the Stream CLA (required)
  • PR targets the develop branch
  • Documentation is updated
  • New code is tested in main example apps, including all possible scenarios
    • SampleApp iOS and Android
    • Expo iOS and Android

@Stream-SDK-Bot
Copy link
Contributor

SDK Size

title develop branch diff status
js_bundle_size 466 KB 466 KB 0 B 🟢

@isekovanic isekovanic merged commit 52168ab into develop Mar 6, 2025
6 checks passed
@isekovanic isekovanic deleted the fix/pagination-breaking-with-offline-support branch March 6, 2025 18:30
@github-actions github-actions bot mentioned this pull request Mar 7, 2025
6 tasks
@stream-ci-bot
Copy link
Contributor

🎉 This PR is included in version 6.6.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

4 participants