Skip to content

Core Data Concurrency update #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 11, 2025
Merged

Conversation

melekr
Copy link
Collaborator

@melekr melekr commented Jan 28, 2025

Refactors PersistentRepository to safely perform Core Data operations.

Why?

When in concurrency debugging mode -com.apple.CoreData.ConcurrencyDebug 1; any direct call into the context methods from an unexpected thread or queue immediately triggers a concurrency violation.
This requires that a private queue context .privateQueueConcurrencyType must be accessed exclusively through its perform(_:) or performAndWait(_:) methods, which schedule all reads/writes on its private serial queue.

Changes:

  • Add NSManagedObjectContext extension to capture any thrown error and return the result
  • Update public Repository methods to use backgroundContext.performAndWaitThrowing
  • Remove direct off-queue fetch/save calls to fix concurrency rule violations
  • Move fetch/delete logic into private _locked helpers
  • Add doc comments
  • Expand Core Data Test coverage

ref: BT-5464

…rmAndWaitThrowing

- Add NSManagedObjectContext extension to handle to capture any thrown error and return the result
- Update public Repository methods to use backgroundContext.performAndWaitThrowing
- Move fetch/delete logic into private _locked helper
- Remove direct off-queue fetch/save calls to fix concurrency rule violations
- Add doc comments
- Add NSManagedObjectContextExtensionTests
- Expand BacktraceDatabaseTests to add concurrent read/write operations, maxRecordCount and retry count Tests
@melekr melekr self-assigned this Jan 28, 2025
return result
}

// Swift 5 approach
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

swift 5 approach is cleaner

try backgroundContext.save()
}
// File storage outside the Core Data backgroundContext (optional concurrency).
// TODO: Verify AttributesStorage for concurrency
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial verification of AttributesStorage did not reveal any issues.

@melekr melekr changed the title Bugfix/persistent repository update Core Data Concurrency update Jan 28, 2025
result = Swift.Result(catching: block)
}
guard let outcome = result else {
throw PerformAndWaitError.blockDidNotRun
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edge case

}

// concurrent reads
for _ in 1...5 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

higher concurrent reads/writes take a lot of time.

@melekr melekr merged commit 6405e83 into master Feb 11, 2025
4 checks passed
@melekr melekr deleted the bugfix/persistent_repository_update branch February 11, 2025 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants