This page provides detailed documentation for the NSManagedObjectContext+Hydrate category.
Installation
- Open your Xcode project.
- Make sure you have the latest version of NSManagedObjectContext+Hydrate. If not, add it to your project’s dependencies.
- Add the following import statement to the file where you want to use this category:
// Swift
import CoreDataHydrate
// Objective-C
#import "NSManagedObjectContext+Hydrate.h"
Usage
Fetching Objects
To fetch objects using the NSManagedObjectContext+Hydrate category, follow the steps below:
- Create a fetch request using the desired entity and any additional criteria:
// Swift
let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "EntityName")
fetchRequest.predicate = NSPredicate(format: "attribute = %@", argumentArray: ["value"])
// Objective-C
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"EntityName"];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"attribute = %@", @"value"];
- Fetch objects using the created fetch request:
// Swift
let managedObjectContext: NSManagedObjectContext = // your managed object context
let fetchedObjects = managedObjectContext.hydrate_objects(with: fetchRequest)
// Objective-C
NSManagedObjectContext *managedObjectContext = // your managed object context
NSArray<NSManagedObject *> *fetchedObjects = [managedObjectContext hydrate_objectsWithFetchRequest:fetchRequest];
Inserting Objects
To insert objects using the NSManagedObjectContext+Hydrate category, follow the steps below:
- Create a new NSManagedObject instance:
// Swift
let entity = NSEntityDescription.entity(forEntityName: "EntityName", in: managedObjectContext)!
let newObject = NSManagedObject(entity: entity, insertInto: managedObjectContext)
// Objective-C
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName" inManagedObjectContext:managedObjectContext];
NSManagedObject *newObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:managedObjectContext];
- Set the properties of the newly created object:
// Swift
newObject.setValue("value", forKey: "attribute")
// Objective-C
[newObject setValue:@"value" forKey:@"attribute"];
Saving Changes
To save changes using the NSManagedObjectContext+Hydrate category, follow the steps below:
- Call the
save
method on your managed object context:
// Swift
managedObjectContext.hydrate_saveChanges()
// Objective-C
[managedObjectContext hydrate_saveChanges];
Removing Objects
To remove objects using the NSManagedObjectContext+Hydrate category, follow the steps below:
- Delete the desired object from the managed object context:
// Swift
managedObjectContext.delete(object)
// Objective-C
[managedObjectContext deleteObject:object];
- Save the changes:
// Swift
managedObjectContext.hydrate_saveChanges()
// Objective-C
[managedObjectContext hydrate_saveChanges];
Conclusion
You now have a solid understanding of how to use the NSManagedObjectContext+Hydrate category to fetch, insert, update, and delete objects within your Core Data stack. Make sure to consult the Core Data documentation for additional information on working with managed object contexts.