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

Cache NSManagedObjectID instead of the CurrentUserDTO #3500

Merged
merged 3 commits into from
Nov 21, 2024

Conversation

laevandus
Copy link
Contributor

@laevandus laevandus commented Nov 19, 2024

🔗 Issue Links

Resolves IOS-21

🎯 Goal

Cache NSManagedObjectID instead of the full CurrentUserDTO for increased reliability.

📝 Summary

  • Cache NSManagedObjectID which is less susceptible to possible threading issues (although I haven't found out that his is actually the case, but something seems to have corrupted the DTO in rare cases)

🛠 Implementation

We have seen a crash when NSManagedObjectContext's currentUser property is accessed which leads to a crash when the user.id is called. This PR tries to fix this issue by not keeping the DTO alive all the time and instead asking context to retrieve it instead of (which is not slow).
I measured the time taken for accessing the currentUser property and when all the call sites are combined, it was 1 ms.

🧪 Manual Testing Notes

Quick testing round of logging in and out

☑️ Contributor Checklist

  • I have signed the Stream CLA (required)
  • This change should be manually QAed
  • Changelog is updated with client-facing changes
  • Changelog is updated with new localization keys
  • New code is covered by unit tests
  • Comparison screenshots added for visual changes
  • Affected documentation updated (docusaurus, tutorial, CMS)

@laevandus laevandus added 🐞 Bug An issue or PR related to a bug 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK labels Nov 19, 2024
@laevandus laevandus requested a review from a team as a code owner November 19, 2024 13:58
@laevandus laevandus added the 🤞 Ready For QA A PR that is Ready for QA label Nov 19, 2024
Copy link

1 Warning
⚠️ The changes should be manually QAed before the Pull Request will be merged

Generated by 🚫 Danger

Comment on lines -126 to 129
let context = databaseContainer.viewContext
if Thread.isMainThread {
currentUserId = context.currentUser?.user.id
} else {
context.performAndWait {
currentUserId = context.currentUser?.user.id
}
databaseContainer.backgroundReadOnlyContext.performAndWait {
currentUserId = databaseContainer.backgroundReadOnlyContext.currentUser?.user.id
}
self.currentUserId = currentUserId
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looked odd that we have such check here, therefore I changed it around.

@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Nov 19, 2024

SDK Size

title develop branch diff status
StreamChat 7.02 MB 7.02 MB 0 KB 🟢
StreamChatUI 4.96 MB 4.96 MB 0 KB 🟢

@Stream-SDK-Bot
Copy link
Collaborator

SDK Performance

target metric benchmark branch performance status
MessageList Hitches total duration 10 ms 6.68 ms 33.2% 🔼 🟢
Duration 2.6 s 2.54 s 2.31% 🔼 🟢
Hitch time ratio 4 ms per s 2.62 ms per s 34.5% 🔼 🟢
Frame rate 75 fps 78.01 fps 4.01% 🔼 🟢
Number of hitches 1 0.8 20.0% 🔼 🟢

Copy link
Member

@nuno-vieira nuno-vieira left a comment

Choose a reason for hiding this comment

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

LGTM! ✅ Much better approach 👌

Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

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

LGTM ✅ There are some failing tests though that need checking.

@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Nov 20, 2024

SDK Size

title develop branch diff status
StreamChat 7.06 MB 7.06 MB 0 KB 🟢
StreamChatUI 4.96 MB 4.96 MB 0 KB 🟢

@laevandus laevandus force-pushed the fix/rare-current-user-crash branch from 1749b7f to 4cda4ec Compare November 21, 2024 07:02
@laevandus laevandus added 🟢 QAed A PR that was QAed and removed 🤞 Ready For QA A PR that is Ready for QA labels Nov 21, 2024
@laevandus
Copy link
Contributor Author

QA was done with @testableapple

@laevandus laevandus merged commit 4d098f5 into develop Nov 21, 2024
13 checks passed
@laevandus laevandus deleted the fix/rare-current-user-crash branch November 21, 2024 10:20
@Stream-SDK-Bot Stream-SDK-Bot mentioned this pull request Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 Bug An issue or PR related to a bug 🟢 QAed A PR that was QAed 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants