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

[CIS-974] Messages maintain correct order of the local time is off #1246

Merged
merged 4 commits into from
Jul 7, 2021

Conversation

VojtaStavik
Copy link
Contributor

[Fixes #1231]

We didn't reset locallyCreatedAt value after the message was sent which was causing the wrong ordering of the messages when the local time was different from the server time.

I also added an artificial time offset for the unsent messages sorting key such that the most recent message is always at the bottom of the list.

@VojtaStavik VojtaStavik requested a review from b-onc July 2, 2021 12:08
@VojtaStavik VojtaStavik changed the title [CIS-974] Message maintain correct order of the local time is off [CIS-974] Messages maintain correct order of the local time is off Jul 2, 2021
@VojtaStavik VojtaStavik force-pushed the CIS-974-local-message-ordering branch from dba55d2 to fb7e84b Compare July 2, 2021 12:12
@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2021

Codecov Report

Merging #1246 (2242d28) into main (00496b8) will increase coverage by 1.17%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1246      +/-   ##
==========================================
+ Coverage   90.34%   91.52%   +1.17%     
==========================================
  Files         174      219      +45     
  Lines        7067     9400    +2333     
==========================================
+ Hits         6385     8603    +2218     
- Misses        682      797     +115     
Flag Coverage Δ
llc-tests 91.36% <ø> (?)
llc-tests-ios12 88.21% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
Sources_v3/StreamChat/Errors/ErrorPayload.swift
...Utils/Logger/StringInterpolation+Extenstions.swift
...at/Utils/Database/NSManagedObject+Extensions.swift
Sources_v3/StreamChat/Workers/UserUpdater.swift
Sources_v3/StreamChat/Models/BanEnabling.swift
...llers/SearchControllers/UserSearchController.swift
Sources_v3/StreamChat/Query/ChannelQuery.swift
...t/Workers/EventObservers/MemberEventObserver.swift
...urces_v3/StreamChat/Database/DTOs/MessageDTO.swift
...ources_v3/StreamChat/Utils/MultipartFormData.swift
... and 383 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c2848d7...2242d28. Read the comment docs.

/// We use this value as the offset for the sorting key for unsent messages. Even if the local time of the device
/// is different from the server time, we want to be sure the newly sent message is always the last one. By applying
/// the same offset to all unsent message, we make sure the order of the messages is maintained.
static let localSortingKeyTimeOffset: TimeInterval = 24 * 60 * 60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should do this, in some cases the failed message will make no sense if re-ordered (in the meantime newer messages will be in the channel).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 👍 We can either ignore this edge case (all iPhones have the time synced remotely by default - you have to explicitly turn this off and then change the time), or we can just make sure the locallyCreatedAt is always bigger than the last message in the channel we locally have. WDYT?

/// Makes sure the `defaultSortingKey` value is computed and set.
fileprivate func prepareDefaultSortKeyIfNeeded() {
let newSortingKey = locallyCreatedAt ?? createdAt
let newSortingKey = locallyCreatedAt.map { $0.addingTimeInterval(Self.localSortingKeyTimeOffset) } ?? createdAt
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

disclaimer: this is unrelated to your change and possible N/A

From a db perspective computing the sort key sounds is very inefficient (all rows/objects will get a computed value which will be used to sort, gets slower as you data set grows). You normally want your sort key to come from an indexed column

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value is not computed on the fly. We add an additional column that's re-computed every time the object is changed (see the willSave method) and the db uses this value for sorting. This is a limitation of CoreData which doesn't allow sorting by more complex expression (see also channels where we sort by max(createdAt, lastMessageAt).

However, this column is not indexed atm and it should be. Fixing 👍

@b-onc b-onc force-pushed the CIS-974-local-message-ordering branch from fb7e84b to 2242d28 Compare July 7, 2021 14:22
Copy link
Contributor Author

@VojtaStavik VojtaStavik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@b-onc b-onc merged commit 3f7211b into main Jul 7, 2021
@b-onc b-onc deleted the CIS-974-local-message-ordering branch July 7, 2021 15:16
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

Successfully merging this pull request may close these issues.

Comments and replies not showing in chronological order (IOS), see the screenshot for reference
4 participants