From cf5901cf80316d932d2e9e22fd79444acee0e464 Mon Sep 17 00:00:00 2001 From: Diego Sanchez Date: Fri, 26 Feb 2016 11:06:07 +0000 Subject: [PATCH] Avoids retaining the view controller until update completes --- .../BaseChatViewController+Changes.swift | 1 + .../BaseChatViewControllerTests.swift | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift index d136da0..add5ace 100644 --- a/Chatto/Source/ChatController/BaseChatViewController+Changes.swift +++ b/Chatto/Source/ChatController/BaseChatViewController+Changes.swift @@ -41,6 +41,7 @@ extension BaseChatViewController: ChatDataSourceDelegateProtocol { let oldItems = sSelf.chatItemCompanionCollection sSelf.updateModels(newItems: newItems, oldItems: oldItems, updateType: updateType, completion: { + guard let sSelf = self else { return } if sSelf.updateQueue.isEmpty { sSelf.enqueueMessageCountReductionIfNeeded() } diff --git a/Chatto/Tests/ChatController/BaseChatViewControllerTests.swift b/Chatto/Tests/ChatController/BaseChatViewControllerTests.swift index fe171d1..216d3a2 100644 --- a/Chatto/Tests/ChatController/BaseChatViewControllerTests.swift +++ b/Chatto/Tests/ChatController/BaseChatViewControllerTests.swift @@ -153,11 +153,10 @@ class ChatViewControllerTests: XCTestCase { } } - func testThat_ControllerDoesNotLeak() { + func testThat_WhenUpdatesFinish_ControllerIsNotRetained() { let asyncExpectation = expectationWithDescription("update") let updateQueue = SerialTaskQueueTestHelper() - let presenterBuilder = FakePresenterBuilder() - var controller: TesteableChatViewController! = TesteableChatViewController(presenterBuilders: ["fake-type": [presenterBuilder]]) + var controller: TesteableChatViewController! = TesteableChatViewController(presenterBuilders: ["fake-type": [FakePresenterBuilder()]]) weak var weakController = controller controller.updateQueue = updateQueue let fakeDataSource = FakeDataSource() @@ -173,6 +172,14 @@ class ChatViewControllerTests: XCTestCase { } } + func testThat_WhenLayoutFinishes_ControllerIsNotRetained() { + var controller: TesteableChatViewController! = TesteableChatViewController(presenterBuilders: ["fake-type": [FakePresenterBuilder()]]) + weak var weakController = controller + controller.chatDataSource = FakeDataSource() + self.fakeDidAppearAndLayout(controller: controller) + controller = nil + XCTAssertNil(weakController) + } func testThat_LayoutAdaptsWhenKeyboardIsShown() { let controller = TesteableChatViewController()