Skip to content

Commit

Permalink
Fix current user cache not deleted on logout causing unread count iss…
Browse files Browse the repository at this point in the history
…ues after switching users (#3055)

* Fix current user cache not delete on logout causing unread count issues after switching users

* Update CHANGELOG.md
  • Loading branch information
nuno-vieira committed Mar 4, 2024
1 parent c1d3dd0 commit 35c366d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### 🐞 Fixed
- Fix token provider retrying after calling disconnect [#3052](https://github.com/GetStream/stream-chat-swift/pull/3052)
- Fix connect user never completing when disconnecting after token provider fails [#3052](https://github.com/GetStream/stream-chat-swift/pull/3052)
- Fix current user cache not deleted on logout causing unread count issues after switching users [#3055](https://github.com/GetStream/stream-chat-swift/pull/3055)

# [4.49.0](https://github.com/GetStream/stream-chat-swift/releases/tag/4.49.0)
_February 27, 2024_
Expand Down
10 changes: 8 additions & 2 deletions Sources/StreamChat/Database/DatabaseContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class DatabaseContainer: NSPersistentContainer {
static var cachedModel: NSManagedObjectModel?

/// All `NSManagedObjectContext`s this container owns.
private lazy var allContext: [NSManagedObjectContext] = [viewContext, backgroundReadOnlyContext, writableContext]
private(set) lazy var allContext: [NSManagedObjectContext] = [viewContext, backgroundReadOnlyContext, writableContext]

/// Creates a new `DatabaseContainer` instance.
///
Expand Down Expand Up @@ -224,8 +224,14 @@ class DatabaseContainer: NSPersistentContainer {

/// Removes all data from the local storage.
func removeAllData(completion: ((Error?) -> Void)? = nil) {
/// Cleanup the current user cache for all manage object contexts.
allContext.forEach { context in
context.perform {
context.invalidateCurrentUserCache()
}
}

writableContext.performAndWait { [weak self] in
self?.writableContext.invalidateCurrentUserCache()
let entityNames = self?.managedObjectModel.entities.compactMap(\.name)
var deleteError: Error?
entityNames?.forEach { [weak self] entityName in
Expand Down
7 changes: 7 additions & 0 deletions Tests/StreamChatTests/Database/DatabaseContainer_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ final class DatabaseContainer_Tests: XCTestCase {
let fetchedObjects = try container.viewContext.fetch(fetchRequrest)
XCTAssertTrue(fetchedObjects.isEmpty)
}

// Assert that currentUser cache has been deleted
container.allContext.forEach { context in
context.performAndWait {
XCTAssertNil(context.currentUser)
}
}
}

func test_databaseContainer_callsResetEphemeralValues_onAllEphemeralValuesContainerEntities() throws {
Expand Down

0 comments on commit 35c366d

Please sign in to comment.