Skip to content

Commit

Permalink
[CIS-1771] Expose method for clearing local DB data (#2241)
Browse files Browse the repository at this point in the history
* Add logout method for properly disconnecting users and clearing local data.

* Update documentation on logout, remove completion from logout and add test.

* Update change log

* Code review fixes

* Update docusaurus/docs/iOS/uikit/getting-started.md

Co-authored-by: Nuno Vieira <nuno.fcvieira93@gmail.com>

Co-authored-by: Nuno Vieira <nuno.fcvieira93@gmail.com>
  • Loading branch information
hugobernalstream and nuno-vieira committed Aug 23, 2022
1 parent 3f748ee commit 86daace
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
# Upcoming

## StreamChat
### ✅ Added
- From now on, if you want to logout the user from the app, especially when switching users, you should call the `client.logout()` method instead of `client.disconnect()`. Read more [here](https://getstream.io/chat/docs/sdk/ios/uikit/getting-started/#disconnect--logout) [#2241](https://github.com/GetStream/stream-chat-swift/pull/2241)
### 🐞 Fixed
- Fix hidden channels showing past history [#2216](https://github.com/GetStream/stream-chat-swift/pull/2216)

Expand Down
3 changes: 2 additions & 1 deletion DemoApp/StreamChat/StreamChatWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ extension StreamChatWrapper {
}
}

client.disconnect()
client.logout()

self.client = nil
}
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/StreamChat/ChatClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ public class ChatClient {
}
userConnectionProvider = nil
}

/// Disconnects the chat client form the chat servers and removes all the local data related.
public func logout() {
disconnect()
databaseContainer.removeAllData(force: true) { error in
if let error = error {
log.error("Logging out current user failed with error \(error)", subsystems: .all)
return
} else {
log.debug("Logging out current user successfully.", subsystems: .all)
}
}
}

func fetchCurrentUserIdFromDatabase() -> UserId? {
var currentUserId: UserId?
Expand Down
15 changes: 15 additions & 0 deletions Tests/StreamChatTests/StreamChat/ChatClient_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,21 @@ final class ChatClient_Tests: XCTestCase {
XCTAssertEqual(testEnv.clientUpdater!.disconnect_source, .userInitiated)
}

func test_logout_disconnectsAndRemovesLocalData() {
// GIVEN
let client = ChatClient(
config: inMemoryStorageConfig,
environment: testEnv.environment
)

// WHEN
client.logout()

// THEN
XCTAssertTrue(testEnv.clientUpdater!.disconnect_called)
XCTAssertTrue(testEnv.databaseContainer!.removeAllData_called)
}

// MARK: - Background workers tests

func test_productionClientIsInitalizedWithAllMandatoryBackgroundWorkers() {
Expand Down
13 changes: 13 additions & 0 deletions docusaurus/docs/iOS/uikit/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ ChatClient.shared.connectUser(userInfo: userInfo, tokenProvider: tokenProvider)
}
```

### Disconnect & Logout

Whenever your users leave the chat component, you should use disconnect to stop receiving chat updates and events while using other features of your app. You disconnect by calling:
```swift
chatClient.disconnect()
```

If your users logout form their account you should use logout instead for completely logging out from the session. You logout by calling:

```swift
chatClient.logout()
```

### Show Channel List

Once the `ChatClient` is connected, we can show the list of channels.
Expand Down

0 comments on commit 86daace

Please sign in to comment.