A collection of lightweight Core Data classes for iOS and OS X.
MDMCoreData is a growing collection of lightweight classes that make working with Core Data easier. It does not try to hide Core Data but instead tries to enforce best practices and reduce boiler plate code. All classes are documented and unit tested.
-
MDMPersistenceController (iOS, OS X) - A lightweight class that sets up an efficient Core Data stack with support for creating multiple child managed object contexts. A private managed object context is used for asynchronous saving. A SQLite store is used for data persistence.
-
MDMFetchedResultsTableDataSource (iOS) - A class mostly full of boiler plate that implements the fetched results controller delegate and a table data source and is used by a table view to access Core Data models.
-
NSManagedObject+MDMCoreDataAdditions (iOS, OS X) - A category on managed objects that provides helper methods for eliminating boiler plate code.
-
...
| iOS | OS X | Documented | Tested | |
|---|---|---|---|---|
| MDMPersistenceController | ✓ | ✓ | ✓ | ✓ |
| MDMFetchedResultsTableDataSource | ✓ | ✓ | ||
| NSManagedObject+MDMCoreDataAdditions | ✓ | ✓ | ✓ | |
| ... |
To run the example project clone the repo and open MDMCoreData.xcworkspace.
To create a new MDMPersistenceController call initWithStoreURL:modelURL: with the URLs of the SQLite file and data model.
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"MDMCoreData.sqlite"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MDMCoreData" withExtension:@"momd"];
self.persistenceController = [[MDMPersistenceController alloc] initWithStoreURL:storeURL
modelURL:modelURL];Easily access the main queue managed object context via the public managedObjectContext property:
self.persistenceController.managedObjectContextTo save changes, call saveContextAndWait:completion: with an optional completion block:
[self.persistenceController saveContextAndWait:NO completion:^(NSError *error) {
if (error == nil) {
NSLog(@"Successfully saved all the things!");
}
}];New child context can be created with the main queue or a private queue for background work:
NSManagedObjectContext *privateContextForScratchPadWork = [self.persistenceController newChildManagedObjectContext];
NSManagedObjectContext *privateContextForDoingBackgroundWork = [self.persistenceController newPrivateChildManagedObjectContext];For more information please see the documentation.
To create a new MDMFetchedResultsTableDataSource call initWithTableView:fetchedResultsController: with a table view and fetched results controller. You also need to set the delegate and reuseIdentifier.
self.tableDataSource = [[MDMFetchedResultsTableDataSource alloc] initWithTableView:self.tableView
fetchedResultsController:[self fetchedResultsController]];
self.tableDataSource.delegate = self;
self.tableDataSource.reuseIdentifier = @"Cell";
self.tableView.dataSource = self.tableDataSource;For cell configuration and object deletion MDMFetchedResultsTableDataSource requires all MDMFetchedResultsTableDataSourceDelegate methods be implemented:
- (void)dataSource:(MDMFetchedResultsTableDataSource *)dataSource
configureCell:(id)cell
withObject:(id)object {
UITableViewCell *tableCell = (UITableViewCell *)cell;
tableCell.textLabel.text = [[object valueForKey:@"timeStamp"] description];
}
- (void)dataSource:(MDMFetchedResultsTableDataSource *)dataSource
deleteObject:(id)object
atIndexPath:(NSIndexPath *)indexPath {
[self.persistenceController.managedObjectContext deleteObject:object];
}During large data imports you can easily pause MDMFetchedResultsTableDataSource for improved performance:
self.tableDataSource.paused = YES;For more information please see the documentation.
Instead of hardcoding an entity name you can call MDMCoreDataAdditionsEntityName:
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[Event MDMCoreDataAdditionsEntityName]];New managed objections can be created in 1 line:
Event *newEvent = [Event MDMCoreDataAdditionsInsertNewObjectIntoContext:[self.fetchedResultsController managedObjectContext]];For more information please see the documentation.
MDMCoreData is available through CocoaPods, to install it simply add the following line to your Podfile:
pod "MDMCoreData"
If you don't need everything, you can install only what you need:
pod "MDMCoreData/MDMPersistenceController"
pod "MDMCoreData/MDMFetchedResultsTableDataSource"
To install manually just copy everything in the Classes directory into your Xcode project.
Important: If your project doesn't use ARC you must add the -fobjc-arc compiler flag to all MDMCoreData implementation files in Target Settings > Build Phases > Compile Sources.
Pull request are welcomed. To add functionality or to make changes:
- Fork this repo
- Open
MDMCoreData.xcworkspacein the Example directory - Make changes to the necessary files in the Pods sub project
- Ensure new public methods are documented and tested
- Submit a pull request
Created by Matthew Morey and other contributors.
MDMCoreData is available under the MIT license. See the LICENSE file for more information. If you're using MDMCoreData in your project, attribution would be nice.
MDMCoreData is based on and inspired by the work of many:

