SimplyPersist
is a Swift package that provides a convenient and efficient way to perform persistence operations using SwiftData.
It allows operation on background threads by implementing the new ModelActor
protocol.
SimplyPersist
is a Swift package designed to facilitate efficient and easy-to-use persistence operations in Swift applications.
It utilizes Swift's concurrency model, including the ModelActor
protocol, to allow safe operations on background threads.
This package is built on top of Swift new data persistence capabilities, providing a streamlined API for managing your data models and performing database operations asynchronously.
PersistenceError
enum for handling specific persistence-related errors, such as noSchema
, ensuring robust error management in your persistence layer.To integrate SimplyPersist
into your Xcode project using Swift Package Manager, follow these steps:
File
> Swift Packages
> Add Package Dependency
.https://github.com/lukacs-m/SimplyPersist
To start using SimplyPersist
, initialize the `PersistenceService with your data models and optional configurations.
import SimplyPersist
// Initialize PersistenceService with models and optional configuration
let persistenceService = try PersistenceService(for: YourModel.self, migrationPlan: YourMigrationPlan.self, configurations: YourConfigurations.self)
Save your data objects asynchronously using the save method.
let model = YourPersistableData()
try await persistenceService.save(data: model)
Fetch data objects asynchronously using predicates for filtering and sorting descriptors for ordering.
// Fetch data with optional predicate and sorting descriptor
let fetchedData = try await persistenceService.fetch(predicate: yourPredicate, sortingDescriptor: yourSortingDescriptor)
"or"
// Fetch a data object by identifier
let fetchedData = try await persistenceService.fetch(identifier: ID)
Delete data objects asynchronously either individually or in bulk.
// Delete a single data object
persistenceService.delete(element: yourPersistableElement)
// Delete all data of specified types
try await persistenceService.deleteAll(dataTypes: [YourPersistableData.Type])
For improved performance, especially with large data sets, use batch saving.
// Batch save an array of data with a specified batch size
try await persistenceService.batchSave(content: yourDataArray, batchSize: yourBatchSize)
PersistenceService
provides a custom error type, PersistenceError
, which includes:
noSchema
: Indicates that no schema is available.
Handle errors using Swift's do-catch mechanism:do {
// Your persistence operations here
} catch let error as PersistenceError {
switch error {
case .noSchema:
print("Error: No schema available.")
// Handle other cases as needed
}
} catch {
print("An unexpected error occurred: \(error)")
}
`SimplyPersist is designed to be flexible and adaptable to various use cases, including more complex data handling and migration scenarios.
Contributions to `SimplyPersist are welcome!
For support, questions, or to report issues, please open an issue on the GitHub repository: https://github.com/lukacs-m/SimplyPersist/issues.