diff --git a/Sources/StreamChat/Controllers/ChannelController/ChannelController.swift b/Sources/StreamChat/Controllers/ChannelController/ChannelController.swift index 4c048b968b..7ee1ae60df 100644 --- a/Sources/StreamChat/Controllers/ChannelController/ChannelController.swift +++ b/Sources/StreamChat/Controllers/ChannelController/ChannelController.swift @@ -1670,17 +1670,8 @@ private extension ChatChannelController { } guard let cid = self.cid else { return nil } let sortAscending = self.messageOrdering == .topToBottom ? false : true - var deletedMessageVisibility: ChatClientConfig.DeletedMessageVisibility? - var shouldShowShadowedMessages: Bool? - self.client.databaseContainer.viewContext.performAndWait { [weak self] in - guard let self = self else { - log.warning("Callback called while self is nil") - return - } - deletedMessageVisibility = self.client.databaseContainer.viewContext.deletedMessagesVisibility - shouldShowShadowedMessages = self.client.databaseContainer.viewContext.shouldShowShadowedMessages - } - + let deletedMessageVisibility = client.config.deletedMessagesVisibility + let shouldShowShadowedMessages = client.config.shouldShowShadowedMessages let pageSize = channelQuery.pagination?.pageSize ?? .messagesPageSize let observer = BackgroundListDatabaseObserver( database: client.databaseContainer, @@ -1688,8 +1679,8 @@ private extension ChatChannelController { for: cid, pageSize: pageSize, sortAscending: sortAscending, - deletedMessagesVisibility: deletedMessageVisibility ?? .visibleForCurrentUser, - shouldShowShadowedMessages: shouldShowShadowedMessages ?? false + deletedMessagesVisibility: deletedMessageVisibility, + shouldShowShadowedMessages: shouldShowShadowedMessages ), itemCreator: { try $0.asModel() as ChatMessage }, itemReuseKeyPaths: (\ChatMessage.id, \MessageDTO.id) diff --git a/Sources/StreamChat/Database/DatabaseContainer.swift b/Sources/StreamChat/Database/DatabaseContainer.swift index f77a83f1d9..eedae2c14d 100644 --- a/Sources/StreamChat/Database/DatabaseContainer.swift +++ b/Sources/StreamChat/Database/DatabaseContainer.swift @@ -284,7 +284,7 @@ class DatabaseContainer: NSPersistentContainer, @unchecked Sendable { } func readAndWait(_ actions: (DatabaseSession) throws -> T) throws -> T { - let context = viewContext + let context = backgroundReadOnlyContext var result: T? var readError: Error? context.performAndWait { diff --git a/Sources/StreamChat/StateLayer/ChatClient+Factory.swift b/Sources/StreamChat/StateLayer/ChatClient+Factory.swift index b6444d86c5..3fe1efcf40 100644 --- a/Sources/StreamChat/StateLayer/ChatClient+Factory.swift +++ b/Sources/StreamChat/StateLayer/ChatClient+Factory.swift @@ -10,8 +10,11 @@ extension ChatClient { /// Creates an instance of ``ConnectedUser`` which represents the logged-in user state and its actions. /// /// - Throws: An error if no user is currently logged-in. - @MainActor public func makeConnectedUser() throws -> ConnectedUser { - let user = try CurrentUserDTO.load(context: databaseContainer.viewContext) + public func makeConnectedUser() throws -> ConnectedUser { + let user = try databaseContainer.readAndWait { session in + guard let dto = session.currentUser else { throw ClientError.CurrentUserDoesNotExist() } + return try dto.asModel() + } return ConnectedUser(user: user, client: self) } } diff --git a/TestTools/StreamChatTestTools/Mocks/StreamChat/ChatClient_Mock.swift b/TestTools/StreamChatTestTools/Mocks/StreamChat/ChatClient_Mock.swift index da564183ed..77796f113d 100644 --- a/TestTools/StreamChatTestTools/Mocks/StreamChat/ChatClient_Mock.swift +++ b/TestTools/StreamChatTestTools/Mocks/StreamChat/ChatClient_Mock.swift @@ -125,7 +125,7 @@ extension ChatClient { } /// Create a new instance of mock `ChatClient` - static func mock(config: ChatClientConfig? = nil, bundle: Bundle? = nil) -> ChatClient { + static func mock(config: ChatClientConfig? = nil, bundle: Bundle? = nil) -> ChatClient_Mock { .init( config: config ?? defaultMockedConfig, environment: .init( diff --git a/Tests/StreamChatTests/Controllers/ChannelController/ChannelController_Tests.swift b/Tests/StreamChatTests/Controllers/ChannelController/ChannelController_Tests.swift index 59a5658222..cfd89dbce0 100644 --- a/Tests/StreamChatTests/Controllers/ChannelController/ChannelController_Tests.swift +++ b/Tests/StreamChatTests/Controllers/ChannelController/ChannelController_Tests.swift @@ -22,8 +22,12 @@ final class ChannelController_Tests: XCTestCase { override func setUp() { super.setUp() + setUp(with: ChatClient_Mock.defaultMockedConfig) + } + + func setUp(with config: ChatClientConfig) { env = TestEnvironment() - client = ChatClient.mock + client = ChatClient.mock(config: config) channelId = ChannelId.unique controller = ChatChannelController( channelQuery: .init(cid: channelId), @@ -1229,7 +1233,9 @@ final class ChannelController_Tests: XCTestCase { func test_deletedMessages_withVisibleForCurrentUser_messageVisibility() throws { // Simulate the config setting - client.databaseContainer.viewContext.deletedMessagesVisibility = .visibleForCurrentUser + var config = ChatClient_Mock.defaultMockedConfig + config.deletedMessagesVisibility = .visibleForCurrentUser + setUp(with: config) let currentUserID: UserId = .unique @@ -1264,7 +1270,9 @@ final class ChannelController_Tests: XCTestCase { func test_deletedMessages_withAlwaysHidden_messageVisibility() throws { // Simulate the config setting - client.databaseContainer.viewContext.deletedMessagesVisibility = .alwaysHidden + var config = ChatClient_Mock.defaultMockedConfig + config.deletedMessagesVisibility = .alwaysHidden + setUp(with: config) let currentUserID: UserId = .unique @@ -1334,7 +1342,9 @@ final class ChannelController_Tests: XCTestCase { func test_shadowedMessages_whenVisible() throws { // Simulate the config setting - client.databaseContainer.viewContext.shouldShowShadowedMessages = true + var config = ChatClient_Mock.defaultMockedConfig + config.shouldShowShadowedMessages = true + setUp(with: config) let currentUserID: UserId = .unique