CoreDataMate is a powerful framework that simplifies the process of working with Core Data in iOS and macOS applications. With its intuitive API and extensive set of features, CoreDataMate makes it easier for developers to handle data persistence and management.
Key Features
- Effortless Core Data setup
- Automatic model generation
- Easy data fetching and querying
- Simple data insertion and deletion
- Relationship management made simple
- Batch processing for improved performance
- Supports both iOS and macOS applications
- Lightweight and easy-to-use API
Getting Started
To start using CoreDataMate in your iOS or macOS project, follow these simple steps:
- Install the framework using
Swift Package Manager
orCocoaPods
. - Import the framework into your project.
- Configure your Core Data model using the provided convenience functions.
- Access your managed object contexts through the CoreDataMate API.
- Fetch, insert, update, and delete data effortlessly using the simplified methods.
Installation
Swift Package Manager
To install CoreDataMate using Swift Package Manager, simply add the following dependency to your Package.swift
file:
// swift-tools-version:5.3
import PackageDescription
let package = Package(
// ...
dependencies: [
.package(url: "https://github.com/example/coredatamate.git", from: "1.0.0")
],
// ...
)
CocoaPods
If you prefer using CocoaPods, add the following line to your Podfile
:
pod 'CoreDataMate', '~> 1.0.0'
Usage
Configuring the Core Data Model
Before using CoreDataMate, you need to configure your Core Data model. CoreDataMate provides convenient functions to generate the necessary boilerplate code.
First, create a new Swift file, and use the following code as a starting point:
import CoreDataMate
class CoreDataStack {
static let shared = CoreDataStack(modelName: "YourModelName")
private let modelName: String
init(modelName: String) {
self.modelName = modelName
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: modelName)
container.loadPersistentStores { (_, error) in
if let error = error as NSError? {
// Handle any error in an appropriate way
fatalError("Unresolved error \(error), \(error.userInfo)")
}
}
return container
}()
}
Replace "YourModelName"
with the actual name of your Core Data model file.
CoreDataMate API Overview
The CoreDataMate API offers a straightforward way to interact with Core Data entities and managed object contexts. Here are some of the main features:
CoreDataStack.shared.persistentContainer
: Access the persistent container for your Core Data stack.CoreDataStack.shared.viewContext
: Get the main managed object context for the UI.CoreDataStack.shared.backgroundContext
: Obtain a background managed object context for heavy work.CoreDataStack.shared.saveContext()
: Save changes to the managed object context.CoreDataStack.shared.rollbackContext()
: Discard unsaved changes in the managed object context.Entity.fetchAll()
: Fetch all entities of a particular type.Entity.fetch(withPredicate:)
: Fetch entities using a predicate to filter the results.Entity.insert()
: Insert a new entity into the managed object context.Entity.delete()
: Delete an entity from the managed object context.Entity.update
: Update the properties of an entity.Entity.batchUpdate(withPredicate:)
: Batch update the properties of multiple entities.
Example Usage
Here is an example showcasing the usage of CoreDataMate:
// Fetch all instances of the "User" entity
let users = User.fetchAll()
// Insert a new user into the managed object context
let newUser = User.insert()
newUser.name = "John Doe"
newUser.age = 30
// Save changes to the managed object context
try CoreDataStack.shared.saveContext()
// Update the name of an existing user
let predicate = NSPredicate(format: "name == %@", "John Doe")
let existingUser = User.fetch(withPredicate: predicate).first
existingUser?.name = "Jane Smith"
// Batch update the age of users older than 40
let agePredicate = NSPredicate(format: "age > 40")
User.batchUpdate(withPredicate: agePredicate) { (user) in
user.age = 50
}
// Delete a user from the managed object context
User.delete(existingUser)
// Save changes to the managed object context
try CoreDataStack.shared.saveContext()
With CoreDataMate, you can streamline your Core Data operations and reduce boilerplate code. It provides a simple and elegant API for managing data persistence in your iOS and macOS applications.