MDMCoreData

A collection of lightweight Core Data classes for iOS and OS X.

295
52
Objective-C

MDMCoreData

A collection of lightweight Core Data classes for iOS and OS X as seen on NSScreencast. Support future development of this project by purchasing Core Data by Tutorials.

Version
Platform
License
Twitter: @xzolian

MDMCoreData is a growing collection of classes that make working with Core Data easier. It does not try to hide Core Data but instead enforces best practices and reduces boiler plate code. It is a much better alternative to using the Xcode Core Data Template. All classes are documented and a majority are unit tested.

  • MDMPersistenceController (iOS, OS X) - A handy controller that sets up an efficient Core Data stack with support for creating multiple child managed object contexts. It has a built-in private managed object context that does asynchronous saving for you with a SQLite store.

  • MDMFetchedResultsTableDataSource (iOS) - A class mostly full of boiler plate that implements the fetched results controller delegate and a table data source.

  • MDMFetchedResultsCollectionDataSource (iOS) - A class mostly full of boiler plate that implements the fetched results controller delegate and a collection data source.

  • 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
MDMFetchedResultsCollectionDataSource
NSManagedObject+MDMCoreDataAdditions

Table of Contents

Usage

To run the example project clone the repo and open MDMCoreData.xcworkspace. A video tutorial is available at NSScreencast.

MDMPersistenceController

Store type - SQLite (NSSQLiteStoreType)

The typical store type used when utilizing Core Data is SQLite. 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];

Store type - InMemory (NSInMemoryStoreType)

An alternative (not a widely used) store type is an InMemory store. This type of store is particularly useful when long term persistence is not required e.g. testing.
To create a new MDMPersistenceController (backed by an inMemoryStore type) call initInMemoryTypeWithModelURL:modelURL with the URL of the data model.

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MDMCoreData" withExtension:@"momd"];
self.persistenceController = [[MDMPersistenceController alloc] initInMemoryTypeWithModelURL:modelURL];

Managed Object Context

Easily access the main queue managed object context via the public managedObjectContext property.

self.persistenceController.managedObjectContext

To save changes, call saveContextAndWait:completion: with an optional completion block. If the first parameter is set to NO, saving is done asynchronously.

[self.persistenceController saveContextAndWait:NO completion:^(NSError *error) {

    if (error == nil) {
        NSLog(@"Successfully saved all the things!");
    }
}];

Child contexts

New child contexts can be created with the main queue or a private queue for background work.

NSManagedObjectContext *privateContextForScratchPadWork = [self.persistenceController newChildManagedObjectContext];

NSManagedObjectContext *privateContextForDoingBackgroundWork = [self.persistenceController newPrivateChildManagedObjectContext];

An independent context (a private queue with a new persistent store coordinator) can be created for scenarios where the background save (file write) operation takes significant time but should not block the fetches (file read) on the main (UI) context.

NSManagedObjectContext *independentContextForDoingBackgroundWork = [self.persistenceController newIndependentManagedObjectContextWithNewPersistentStoreCoordinator];

For more information please see the documentation.

MDMFetchedResults[Table|Collection]DataSource

To create a new MDMFetchedResultsTableDataSource or MDMFetchedResultsCollectionViewDataSource call initWithTableView:fetchedResultsController: or initWithCollectionView:fetchedResultsController:with a table view or collection 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 that 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.

NSManagedObject+MDMCoreDataAdditions

Instead of hardcoding an entity name you can call MDMCoreDataAdditionsEntityName.

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[Event MDMCoreDataAdditionsEntityName]];

New managed objects can be created with only one line of code.

Event *newEvent = [Event MDMCoreDataAdditionsInsertNewObjectIntoContext:[self.fetchedResultsController managedObjectContext]];

For more information please see the documentation.

Installation

CocoaPods

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 using separate sub-pods.

pod "MDMCoreData/MDMPersistenceController"
pod "MDMCoreData/MDMFetchedResultsTableDataSource"

Manually

To install manually, just copy everything in the Classes directory into your Xcode project.

Contributing

Pull requests are welcomed. To add functionality or to make changes:

  1. Fork this repo.
  2. Open MDMCoreData.xcworkspace in the Example directory.
  3. Make changes to the necessary files in the Pods sub project.
  4. Ensure new public methods are documented and tested.
  5. Submit a pull request.

You can also support future development of this project by purchasing Core Data by Tutorials.

Author

Created by Matthew Morey, Terry Lewis II, Matt Glover and other contributors.

License

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.

Attribution

MDMCoreData is based on and inspired by the work of many: