Consider the following function snippet:
public func update(tables: [LocalTableItem]) async throws -> UpdateResult {
let result = try await store.remove(tables: tables)
print("** removing table \(tables.last!.name)")
switch result {
case .success:
let insertResult = try await self.store.insert(tables: tables)
print("** inserting table \(tables.last!.name)")
when called in a concurrent manner:
Task {
_ = try await store.update(tables: [updatedTable])
}
I observe in the Log:
** removing table icradle_loler_engineer.
** removing table icradle_loler_engineer.
** inserting table icradle_loler_engineer.
** inserting table icradle_loler_engineer.
The remove and insert calls are no longer balanced and duplicate entries will be seen in the DB.
Is there a way to make this async function thread safe?
I have tried making the class an Actor although my understanding may be incomplete, as this did not resolve the issue.