From 0f1dab4434ce2042d57463a88c8eed8cabc2cfe9 Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Sun, 11 Jun 2023 23:21:19 -0500 Subject: [PATCH 1/4] Deprecate batch read with unused transactionAuthor. Add batch read with no transactionAuthor parameter. accidental-transaction-author-parameter-on-batch-read --- .../CoreDataRepository+Batch.swift | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Sources/CoreDataRepository/CoreDataRepository+Batch.swift b/Sources/CoreDataRepository/CoreDataRepository+Batch.swift index 20c7aa9..b644fde 100644 --- a/Sources/CoreDataRepository/CoreDataRepository+Batch.swift +++ b/Sources/CoreDataRepository/CoreDataRepository+Batch.swift @@ -82,6 +82,7 @@ extension CoreDataRepository { /// - urls: [URL] /// - Returns /// - (success: [Model, failed: [Model]) + @available(*, deprecated, message: "This method has an unused parameter for transactionAuthor.") public func read( urls: [URL], transactionAuthor _: String? = nil @@ -119,6 +120,45 @@ extension CoreDataRepository { return (success: successes, failed: failures) } + /// Batch update objects in CoreData + /// - Parameters + /// - urls: [URL] + /// - Returns + /// - (success: [Model, failed: [Model]) + public func read(urls: [URL]) async -> (success: [Model], failed: [URL]) { + var successes = [Model]() + var failures = [URL]() + await withTaskGroup(of: _Result.self, body: { [weak self] group in + guard let self = self else { + group.cancelAll() + return + } + for url in urls { + let added = group.addTaskUnlessCancelled { + async let result: Result = self.read(url) + switch await result { + case let .success(created): + return _Result.success(created) + case .failure: + return _Result.failure(url) + } + } + if !added { + return + } + } + for await result in group { + switch result { + case let .success(success): + successes.append(success) + case let .failure(failure): + failures.append(failure) + } + } + }) + return (success: successes, failed: failures) + } + /// Batch update objects in CoreData /// - Parameters /// - _ request: NSBatchInsertRequest From f47248b2f965458f7de736486832f5296bfca4e5 Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Mon, 12 Jun 2023 00:37:45 -0500 Subject: [PATCH 2/4] Replace body of deprecated batch read from non-deprecated overload accidental-transaction-author-parameter-on-batch-read --- .../CoreDataRepository+Batch.swift | 32 +------------------ 1 file changed, 1 insertion(+), 31 deletions(-) diff --git a/Sources/CoreDataRepository/CoreDataRepository+Batch.swift b/Sources/CoreDataRepository/CoreDataRepository+Batch.swift index b644fde..59f819b 100644 --- a/Sources/CoreDataRepository/CoreDataRepository+Batch.swift +++ b/Sources/CoreDataRepository/CoreDataRepository+Batch.swift @@ -87,37 +87,7 @@ extension CoreDataRepository { urls: [URL], transactionAuthor _: String? = nil ) async -> (success: [Model], failed: [URL]) { - var successes = [Model]() - var failures = [URL]() - await withTaskGroup(of: _Result.self, body: { [weak self] group in - guard let self = self else { - group.cancelAll() - return - } - for url in urls { - let added = group.addTaskUnlessCancelled { - async let result: Result = self.read(url) - switch await result { - case let .success(created): - return _Result.success(created) - case .failure: - return _Result.failure(url) - } - } - if !added { - return - } - } - for await result in group { - switch result { - case let .success(success): - successes.append(success) - case let .failure(failure): - failures.append(failure) - } - } - }) - return (success: successes, failed: failures) + await read(urls: urls) } /// Batch update objects in CoreData From 37d0bb2edf6bdcba97b0f3be0d0a4aa9892f2dd0 Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Mon, 12 Jun 2023 00:38:02 -0500 Subject: [PATCH 3/4] Add test for deprecated batch read accidental-transaction-author-parameter-on-batch-read --- .../BatchRepositoryTests.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift b/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift index 6159129..42e26c3 100644 --- a/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift +++ b/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift @@ -141,6 +141,27 @@ final class BatchRepositoryTests: CoreDataXCTestCase { try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp) } + + func testDeprecatedReadSuccess() async throws { + let fetchRequest = NSFetchRequest(entityName: "RepoMovie") + var movies = [Movie]() + try await repositoryContext().perform { + let count = try self.repositoryContext().count(for: fetchRequest) + XCTAssertEqual(count, 0, "Count of objects in CoreData should be zero at the start of each test.") + + let repoMovies = try self.movies + .map(self.mapDictToRepoMovie(_:)) + try self.repositoryContext().save() + movies = repoMovies.map(\.asUnmanaged) + } + + let result: (success: [Movie], failed: [URL]) = try await repository().read(urls: movies.compactMap(\.url), transactionAuthor: "Unused") + + XCTAssertEqual(result.success.count, movies.count) + XCTAssertEqual(result.failed.count, 0) + + XCTAssertEqual(Set(movies), Set(result.success)) + } func testReadSuccess() async throws { let fetchRequest = NSFetchRequest(entityName: "RepoMovie") From d2019540f00417f5961555c4ed90538c205c508d Mon Sep 17 00:00:00 2001 From: Andrew Roan Date: Mon, 12 Jun 2023 00:38:26 -0500 Subject: [PATCH 4/4] Run swiftformat accidental-transaction-author-parameter-on-batch-read --- Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift b/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift index 42e26c3..ad5124e 100644 --- a/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift +++ b/Tests/CoreDataRepositoryTests/BatchRepositoryTests.swift @@ -141,7 +141,7 @@ final class BatchRepositoryTests: CoreDataXCTestCase { try verify(transactionAuthor: transactionAuthor, timeStamp: historyTimeStamp) } - + func testDeprecatedReadSuccess() async throws { let fetchRequest = NSFetchRequest(entityName: "RepoMovie") var movies = [Movie]() @@ -155,7 +155,8 @@ final class BatchRepositoryTests: CoreDataXCTestCase { movies = repoMovies.map(\.asUnmanaged) } - let result: (success: [Movie], failed: [URL]) = try await repository().read(urls: movies.compactMap(\.url), transactionAuthor: "Unused") + let result: (success: [Movie], failed: [URL]) = try await repository() + .read(urls: movies.compactMap(\.url), transactionAuthor: "Unused") XCTAssertEqual(result.success.count, movies.count) XCTAssertEqual(result.failed.count, 0)