Skip to content

sallyransom/MDMCoreData

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MDMCoreData

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

Version Platform

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
...

Usage

To run the example project clone the repo and open MDMCoreData.xcworkspace.

MDMPersistenceController

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.managedObjectContext

To 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.

MDMFetchedResultsTableDataSource

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.

NSManagedObject+MDMCoreDataAdditions

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.

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:

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

Manually

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.

Contributing

Pull request 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

Author

Created by Matthew Morey 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:

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Objective-C 83.3%
  • Ruby 9.9%
  • Shell 4.0%
  • C 2.2%
  • C++ 0.6%