Skip to content

Commit

Permalink
Do not use view context in StreamChat (#3550)
Browse files Browse the repository at this point in the history
  • Loading branch information
laevandus authored Jan 4, 2025
1 parent 0485e99 commit e03357e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1670,26 +1670,17 @@ 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,
fetchRequest: MessageDTO.messagesFetchRequest(
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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/StreamChat/Database/DatabaseContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class DatabaseContainer: NSPersistentContainer, @unchecked Sendable {
}

func readAndWait<T>(_ actions: (DatabaseSession) throws -> T) throws -> T {
let context = viewContext
let context = backgroundReadOnlyContext
var result: T?
var readError: Error?
context.performAndWait {
Expand Down
7 changes: 5 additions & 2 deletions Sources/StreamChat/StateLayer/ChatClient+Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e03357e

Please sign in to comment.