Welcome to the documentation for the RWPCoreData framework. This framework provides a set of useful classes and utilities for working with Core Data in your iOS and macOS applications. Whether you are a beginner or an experienced developer, this documentation will guide you through the usage of various features of RWPCoreData.
Table of Contents
- Installation
- Usage
- Core Data Stack
- Managed Object Model
- Managed Object Context
- Fetching Objects
- Creating and Updating Objects
- Deleting Objects
- Examples
- Contributing
- License
Installation
You can install RWPCoreData either manually or using CocoaPods, depending on your preference.
Manual Installation:
1. Download the latest version of RWPCoreData from the GitHub repository.
2. Unzip the downloaded file.
3. Drag and drop the RWPCoreData folder into your Xcode project.
4. Make sure to check the "Copy items if needed" option.
5. Import the framework in your code using import RWPCoreData
Installation with CocoaPods:
1. Open your terminal and navigate to your project's directory.
2. Run pod init
to create a Podfile.
3. Open the Podfile and add the following line: pod 'RWPCoreData'
.
4. Save the Podfile and run pod install
.
5. Close Xcode and open the .xcworkspace
file instead of the .xcodeproj
file from now on.
Usage
Core Data Stack
The Core Data stack is responsible for setting up the persistent store coordinator, managed object model, and managed object context.
// Initialize the core data stack
let coreDataStack = RWPCoreDataStack()
// Accessing the managed object model, coordinator, and context
let managedObjectModel = coreDataStack.managedObjectModel
let persistentStoreCoordinator = coreDataStack.persistentStoreCoordinator
let managedObjectContext = coreDataStack.managedObjectContext
Managed Object Model
The managed object model represents the data model schema in your application.
// Get the managed object model
let managedObjectModel = coreDataStack.managedObjectModel
Managed Object Context
A managed object context is used to perform various operations like fetching, creating, updating, and deleting managed objects.
// Get the managed object context
let managedObjectContext = coreDataStack.managedObjectContext
Fetching Objects
You can fetch objects from Core Data using various fetch request types like simple, sorted, or filtered fetch requests.
// Simple fetch request
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Person")
let results = managedObjectContext.fetch(fetchRequest)
// Sorted fetch request
let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
fetchRequest.sortDescriptors = [sortDescriptor]
let sortedResults = managedObjectContext.fetch(fetchRequest)
// Predicate fetch request
let predicate = NSPredicate(format: "age >= %@", argumentArray: [25])
fetchRequest.predicate = predicate
let filteredResults = managedObjectContext.fetch(fetchRequest)
Creating and Updating Objects
You can create and update managed objects with ease using the managed object context.
// Create a new managed object
guard let entity = NSEntityDescription.entity(forEntityName: "Person", in: managedObjectContext) else {
return
}
let person = NSManagedObject(entity: entity, insertInto: managedObjectContext)
person.setValue("John", forKey: "name")
person.setValue(30, forKey: "age")
// Update an existing managed object
person.setValue("Updated Name", forKey: "name")
Deleting Objects
To delete managed objects, call the delete method on the managed object context.
// Delete a managed object
managedObjectContext.delete(person)
Examples
For examples on how to use RWPCoreData in your applications, please refer to the GitHub repository for this documentation.
Contributing
If you’d like to contribute to RWPCoreData, please follow the guidelines provided in the GitHub repository. Any contributions are greatly appreciated!
License
RWPCoreData is licensed under the MIT license. See the LICENSE file for more information.