diff --git a/CoreStoreTests/BaseTestCase.swift b/CoreStoreTests/BaseTestCase.swift index bdb2d97..f4c5daf 100644 --- a/CoreStoreTests/BaseTestCase.swift +++ b/CoreStoreTests/BaseTestCase.swift @@ -62,6 +62,9 @@ class BaseTestCase: XCTestCase { XCTFail(error.coreStoreDumpString) } + self.addTeardownBlock { + stack.unsafeRemoveAllPersistentStoresAndWait() + } } @nonobjc diff --git a/CoreStoreTests/DynamicModelTests.swift b/CoreStoreTests/DynamicModelTests.swift index d794f62..1c503db 100644 --- a/CoreStoreTests/DynamicModelTests.swift +++ b/CoreStoreTests/DynamicModelTests.swift @@ -505,7 +505,12 @@ class DynamicModelTests: BaseTestDataTestCase { XCTFail() } ) - self.waitAndCheckExpectations() + } + + self.waitForExpectations(timeout: 10, handler: { _ in }) + + self.addTeardownBlock { + dataStack.unsafeRemoveAllPersistentStoresAndWait() } } diff --git a/CoreStoreTests/FetchTests.swift b/CoreStoreTests/FetchTests.swift index a1676a4..aa1a195 100644 --- a/CoreStoreTests/FetchTests.swift +++ b/CoreStoreTests/FetchTests.swift @@ -132,8 +132,8 @@ final class FetchTests: BaseTestDataTestCase { } ) } + self.waitAndCheckExpectations() } - self.waitAndCheckExpectations() } @objc @@ -267,8 +267,8 @@ final class FetchTests: BaseTestDataTestCase { } ) } + self.waitAndCheckExpectations() } - self.waitAndCheckExpectations() } @objc diff --git a/CoreStoreTests/ObjectPublisherTests.swift b/CoreStoreTests/ObjectPublisherTests.swift index 16460d7..58d4817 100644 --- a/CoreStoreTests/ObjectPublisherTests.swift +++ b/CoreStoreTests/ObjectPublisherTests.swift @@ -144,6 +144,7 @@ class ObjectPublisherTests: BaseTestDataTestCase { XCTFail() } ) + self.waitAndCheckExpectations() withExtendedLifetime(objectPublisher, {}) diff --git a/CoreStoreTests/TransactionTests.swift b/CoreStoreTests/TransactionTests.swift index e91bfb7..2477569 100644 --- a/CoreStoreTests/TransactionTests.swift +++ b/CoreStoreTests/TransactionTests.swift @@ -622,8 +622,8 @@ final class TransactionTests: BaseTestCase { } ) } + self.waitAndCheckExpectations() } - self.waitAndCheckExpectations() } @objc @@ -755,8 +755,8 @@ final class TransactionTests: BaseTestCase { } ) } + self.waitAndCheckExpectations() } - self.waitAndCheckExpectations() } @objc @@ -896,8 +896,8 @@ final class TransactionTests: BaseTestCase { } ) } + self.waitAndCheckExpectations() } - self.waitAndCheckExpectations() } @objc diff --git a/Sources/DataStack.swift b/Sources/DataStack.swift index 3010cda..962acf7 100644 --- a/Sources/DataStack.swift +++ b/Sources/DataStack.swift @@ -370,6 +370,44 @@ public final class DataStack: Equatable { } } + /** + Prepares deinitializing the `DataStack` by removing all persistent stores. This is not necessary, but can help silence SQLite warnings when actively releasing and recreating `DataStack`s. + - parameter completion: the closure to execute after all persistent stores are removed + */ + public func unsafeRemoveAllPersistentStores(completion: @escaping () -> Void = {}) { + + let coordinator = self.coordinator + coordinator.performAsynchronously { + + withExtendedLifetime(coordinator) { coordinator in + + coordinator.persistentStores.forEach { + + _ = try? coordinator.remove($0) + } + } + DispatchQueue.main.async(execute: completion) + } + } + + /** + Prepares deinitializing the `DataStack` by removing all persistent stores. This is not necessary, but can help silence SQLite warnings when actively releasing and recreating `DataStack`s. + */ + public func unsafeRemoveAllPersistentStoresAndWait() { + + let coordinator = self.coordinator + coordinator.performSynchronously { + + withExtendedLifetime(coordinator) { coordinator in + + coordinator.persistentStores.forEach { + + _ = try? coordinator.remove($0) + } + } + } + } + // MARK: 3rd Party Utilities @@ -509,16 +547,6 @@ public final class DataStack: Equatable { deinit { - let coordinator = self.coordinator - coordinator.performAsynchronously { - - withExtendedLifetime(coordinator) { coordinator in - - coordinator.persistentStores.forEach { - - _ = try? coordinator.remove($0) - } - } - } + self.unsafeRemoveAllPersistentStores() } }