Skip to content

Commit 57b91a6

Browse files
committed
Fix application of transactionAuthor to repository context in all editing functions
transaction-author-is-not-applied-to-the-context-for-some-functions
1 parent 600dba8 commit 57b91a6

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

Sources/CoreDataRepository/CoreDataRepository+Batch.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ extension CoreDataRepository {
2222
_ request: NSBatchInsertRequest,
2323
transactionAuthor: String? = nil
2424
) async -> Result<NSBatchInsertResult, CoreDataRepositoryError> {
25-
await context.performInScratchPad { scratchPad in
26-
scratchPad.transactionAuthor = transactionAuthor
25+
await context.performInScratchPad { [context] scratchPad in
26+
context.transactionAuthor = transactionAuthor
2727
guard let result = try scratchPad.execute(request) as? NSBatchInsertResult else {
28+
context.transactionAuthor = nil
2829
throw CoreDataRepositoryError.fetchedObjectFailedToCastToExpectedType
2930
}
31+
context.transactionAuthor = nil
3032
return result
3133
}
3234
}
@@ -127,11 +129,13 @@ extension CoreDataRepository {
127129
_ request: NSBatchUpdateRequest,
128130
transactionAuthor: String? = nil
129131
) async -> Result<NSBatchUpdateResult, CoreDataRepositoryError> {
130-
await context.performInScratchPad { scratchPad in
131-
scratchPad.transactionAuthor = transactionAuthor
132+
await context.performInScratchPad { [context] scratchPad in
133+
context.transactionAuthor = transactionAuthor
132134
guard let result = try scratchPad.execute(request) as? NSBatchUpdateResult else {
135+
context.transactionAuthor = nil
133136
throw CoreDataRepositoryError.fetchedObjectFailedToCastToExpectedType
134137
}
138+
context.transactionAuthor = nil
135139
return result
136140
}
137141
}
@@ -193,11 +197,13 @@ extension CoreDataRepository {
193197
_ request: NSBatchDeleteRequest,
194198
transactionAuthor: String? = nil
195199
) async -> Result<NSBatchDeleteResult, CoreDataRepositoryError> {
196-
await context.performInScratchPad { scratchPad in
197-
scratchPad.transactionAuthor = transactionAuthor
200+
await context.performInScratchPad { [context] scratchPad in
201+
context.transactionAuthor = transactionAuthor
198202
guard let result = try scratchPad.execute(request) as? NSBatchDeleteResult else {
203+
context.transactionAuthor = nil
199204
throw CoreDataRepositoryError.fetchedObjectFailedToCastToExpectedType
200205
}
206+
context.transactionAuthor = nil
201207
return result
202208
}
203209
}

Sources/CoreDataRepository/CoreDataRepository+CRUD.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ extension CoreDataRepository {
2727
transactionAuthor: String? = nil
2828
) async -> Result<Model, CoreDataRepositoryError> {
2929
await context.performInScratchPad(schedule: .enqueued) { [context] scratchPad in
30-
scratchPad.transactionAuthor = transactionAuthor
3130
let object = Model.RepoManaged(context: scratchPad)
3231
object.create(from: item)
3332
try scratchPad.save()
3433
try context.performAndWait {
34+
context.transactionAuthor = transactionAuthor
3535
try context.save()
36+
context.transactionAuthor = nil
3637
}
3738
try scratchPad.obtainPermanentIDs(for: [object])
3839
return object.asUnmanaged
@@ -70,16 +71,19 @@ extension CoreDataRepository {
7071
public func update<Model: UnmanagedModel>(
7172
_ url: URL,
7273
with item: Model,
73-
transactionAuthor _: String? = nil
74+
transactionAuthor: String? = nil
7475
) async -> Result<Model, CoreDataRepositoryError> {
7576
await context.performInScratchPad(schedule: .enqueued) { [context] scratchPad in
77+
scratchPad.transactionAuthor = transactionAuthor
7678
let id = try scratchPad.tryObjectId(from: url)
7779
let object = try scratchPad.notDeletedObject(for: id)
7880
let repoManaged: Model.RepoManaged = try object.asRepoManaged()
7981
repoManaged.update(from: item)
8082
try scratchPad.save()
8183
try context.performAndWait {
84+
context.transactionAuthor = transactionAuthor
8285
try context.save()
86+
context.transactionAuthor = nil
8387
}
8488
return repoManaged.asUnmanaged
8589
}
@@ -97,16 +101,19 @@ extension CoreDataRepository {
97101
///
98102
public func delete(
99103
_ url: URL,
100-
transactionAuthor _: String? = nil
104+
transactionAuthor: String? = nil
101105
) async -> Result<Void, CoreDataRepositoryError> {
102106
await context.performInScratchPad(schedule: .enqueued) { [context] scratchPad in
107+
scratchPad.transactionAuthor = transactionAuthor
103108
let id = try scratchPad.tryObjectId(from: url)
104109
let object = try scratchPad.notDeletedObject(for: id)
105110
object.prepareForDeletion()
106111
scratchPad.delete(object)
107112
try scratchPad.save()
108113
try context.performAndWait {
114+
context.transactionAuthor = transactionAuthor
109115
try context.save()
116+
context.transactionAuthor = nil
110117
}
111118
return ()
112119
}

0 commit comments

Comments
 (0)