Coder Social home page Coder Social logo

mdabbagh88 / mdmcoredata Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mmorey/mdmcoredata

0.0 1.0 0.0 3.9 MB

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

License: MIT License

Objective-C 87.89% Ruby 6.94% C 2.38% Shell 2.79%

mdmcoredata's Introduction

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

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. 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!");
    }
}];

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];

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.

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.

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:

mdmcoredata's People

Contributors

mmorey avatar mattglover avatar tlewisii avatar cananito avatar mralek avatar craigmarvelley avatar gonghao avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.