This package provides a streamlined way to manage Core Data interactions for a notes application. Designed as part of a Swift showcase project, it encapsulates Core Data operations in a clean, modern API, supporting various interaction styles including asynchronous, closure-based, and Combine-based approaches.
- Asynchronous Operations: Leverage Swift's
async/awaitsyntax for modern, clean, and scalable code. - Combine Support: Use Combine publishers to reactively manage Core Data operations.
- Completion Handlers: Optionally use closure-based APIs for compatibility with older codebases.
- Type Safety: Ensure consistent and type-safe interaction with your data layer.
This package is distributed as a Swift Package Manager (SPM) package. To include it in your project:
- In Xcode, go to File > Add Packages....
- Enter the repository URL of this package.
- Choose the version or branch you want to use.
- Add the package to your target.
import NotesStorageThe NotesStorage class provides a public interface for managing notes. It encapsulates the StorageService and offers methods for asynchronous, closure-based, and Combine-based operations.
func create(_ note: NoteViewModel) async throws
func read() throws -> [NoteViewModel]
func update(_ note: NoteViewModel) async throws
func delete(_ notes: [NoteViewModel]) async throwsExample:
let notesStorage = NotesStorage()
let newNote = NoteViewModel(id: UUID(), title: "New Note", body: "This is a test note.")
do {
try await notesStorage.create(newNote)
let notes = try notesStorage.read()
print("Notes: \(notes)")
} catch {
print("An error occurred: \(error)")
}func create(_ note: NoteViewModel, completion: @escaping (Result<Void, any Error>) -> Void)
func update(_ note: NoteViewModel, completion: @escaping (Result<Void, any Error>) -> Void)
func delete(_ notes: [NoteViewModel], completion: @escaping (Result<Void, any Error>) -> Void)Example:
notesStorage.create(newNote) { result in
switch result {
case .success:
print("Note created successfully.")
case .failure(let error):
print("Failed to create note: \(error)")
}
}func createPublisher(_ note: NoteViewModel) -> AnyPublisher<Void, any Error>
func updatePublisher(_ note: NoteViewModel) -> AnyPublisher<Void, any Error>
func deletePublisher(_ notes: [NoteViewModel]) -> AnyPublisher<Void, any Error>Example:
let cancellable = notesStorage.createPublisher(newNote)
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("Note created successfully.")
case .failure(let error):
print("Failed to create note: \(error)")
}
}, receiveValue: {
print("Create operation completed.")
})This package is open source and available under the MIT License.
For any questions or contributions, feel free to open an issue or submit a pull request.