Skip to content

Commit

Permalink
Fix CoreData misuse when creating Direct Messaging channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Bahadir Oncel committed Aug 2, 2021
1 parent a5efc29 commit 8cda2c5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Fix compilation for Xcode 13 beta 3 where SDK could not compile because of unvailability of `UIApplication.shared` [#1333](https://github.com/GetStream/stream-chat-swift/pull/1333)
- Fix member removed from a Channel is still present is MemberListController.members [#1323](https://github.com/GetStream/stream-chat-swift/issues/1323)
- Fix composer input field height for long text [#1335](https://github.com/GetStream/stream-chat-swift/issues/1335)
- Fix creating direct messaging channels creates CoreData misuse [#1337](https://github.com/GetStream/stream-chat-swift/issues/1337)

### 🔄 Changed
- `ContainerStackView` doesn't `assert` when trying to remove a subview, these operations are now no-op [#1328](https://github.com/GetStream/stream-chat-swift/issues/1328)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,17 @@ public class _ChatChannelController<ExtraData: ExtraDataTypes>: DataController,
_messagesObserver.computeValue = { [unowned self] in
guard let cid = self.cid else { return nil }
let sortAscending = self.listOrdering == .topToBottom ? false : true
let deletedMessageVisibility = self.client.databaseContainer.viewContext
.deletedMessagesVisibility ?? .visibleForCurrentUser
var deletedMessageVisibility: ChatClientConfig.DeletedMessageVisibility?
self.client.databaseContainer.viewContext.performAndWait {
deletedMessageVisibility = self.client.databaseContainer.viewContext.deletedMessagesVisibility
}

let observer = ListDatabaseObserver(
context: self.client.databaseContainer.viewContext,
fetchRequest: MessageDTO.messagesFetchRequest(
for: cid,
sortAscending: sortAscending,
deletedMessagesVisibility: deletedMessageVisibility
deletedMessagesVisibility: deletedMessageVisibility ?? .visibleForCurrentUser
),
itemCreator: { $0.asModel() as _ChatMessage<ExtraData> }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,17 @@ class ChannelController_Tests: StressTestCase {
try client.databaseContainer.writeSynchronously {
try $0.saveChannel(payload: payload, query: nil)
}
env.channelUpdater?.update_channelCreatedCallback?(channelId)
env.channelUpdater?.update_completion?(.success(dummyPayload(with: .unique)))

// We call these callbacks on a queue other than main queue
// to simulate the actual scenario where callbacks will be called
// from NSURLSession-delegate (serial) queue
let _: Bool = try waitFor { completion in
DispatchQueue.global().async {
self.env.channelUpdater?.update_channelCreatedCallback?(self.channelId)
self.env.channelUpdater?.update_completion?(.success(self.dummyPayload(with: .unique)))
completion(true)
}
}

XCTAssertEqual(controller.channel?.cid, channelId)
XCTAssertEqual(controller.messages.count, payload.messages.count)
Expand Down

0 comments on commit 8cda2c5

Please sign in to comment.