remove persistentstores during DataStack deinitialization to prevent warnings in Unit tests during teardown

This commit is contained in:
John Estropia
2020-05-26 09:43:39 +09:00
parent 0eb9b6393d
commit 4a97d5a8dc
6 changed files with 54 additions and 17 deletions

View File

@@ -62,6 +62,9 @@ class BaseTestCase: XCTestCase {
XCTFail(error.coreStoreDumpString) XCTFail(error.coreStoreDumpString)
} }
self.addTeardownBlock {
stack.unsafeRemoveAllPersistentStoresAndWait()
}
} }
@nonobjc @nonobjc

View File

@@ -505,7 +505,12 @@ class DynamicModelTests: BaseTestDataTestCase {
XCTFail() XCTFail()
} }
) )
self.waitAndCheckExpectations() }
self.waitForExpectations(timeout: 10, handler: { _ in })
self.addTeardownBlock {
dataStack.unsafeRemoveAllPersistentStoresAndWait()
} }
} }

View File

@@ -132,9 +132,9 @@ final class FetchTests: BaseTestDataTestCase {
} }
) )
} }
}
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
} }
}
@objc @objc
dynamic func test_ThatDataStacksAndTransactions_CanFetchAllExisting() { dynamic func test_ThatDataStacksAndTransactions_CanFetchAllExisting() {
@@ -267,9 +267,9 @@ final class FetchTests: BaseTestDataTestCase {
} }
) )
} }
}
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
} }
}
@objc @objc
dynamic func test_ThatDataStacks_CanFetchOneFromDefaultConfiguration() { dynamic func test_ThatDataStacks_CanFetchOneFromDefaultConfiguration() {

View File

@@ -144,6 +144,7 @@ class ObjectPublisherTests: BaseTestDataTestCase {
XCTFail() XCTFail()
} }
) )
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
withExtendedLifetime(objectPublisher, {}) withExtendedLifetime(objectPublisher, {})

View File

@@ -622,9 +622,9 @@ final class TransactionTests: BaseTestCase {
} }
) )
} }
}
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
} }
}
@objc @objc
dynamic func test_ThatAsynchronousTransactions_CanPerformCRUDsInCorrectConfiguration() { dynamic func test_ThatAsynchronousTransactions_CanPerformCRUDsInCorrectConfiguration() {
@@ -755,9 +755,9 @@ final class TransactionTests: BaseTestCase {
} }
) )
} }
}
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
} }
}
@objc @objc
dynamic func test_ThatAsynchronousTransactions_CanDiscardUncommittedChanges() { dynamic func test_ThatAsynchronousTransactions_CanDiscardUncommittedChanges() {
@@ -896,9 +896,9 @@ final class TransactionTests: BaseTestCase {
} }
) )
} }
}
self.waitAndCheckExpectations() self.waitAndCheckExpectations()
} }
}
@objc @objc
dynamic func test_ThatUnsafeTransactions_CanPerformCRUDs() { dynamic func test_ThatUnsafeTransactions_CanPerformCRUDs() {

View File

@@ -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 // MARK: 3rd Party Utilities
@@ -509,16 +547,6 @@ public final class DataStack: Equatable {
deinit { deinit {
let coordinator = self.coordinator self.unsafeRemoveAllPersistentStores()
coordinator.performAsynchronously {
withExtendedLifetime(coordinator) { coordinator in
coordinator.persistentStores.forEach {
_ = try? coordinator.remove($0)
}
}
}
} }
} }