Skip to content

VivekAgar/CoreDataFullStack-iOS

 
 

Repository files navigation

CoreDataFullStack-iOS

Version License Platform CocoaPods Build Status

What is it?

Easiest way to build a Core Data full stack.

Stack

This project stack consists of two independent managed object contexts which are both connected to the same persistent store coordinator.

alt tag

The Core Data stack used follows the Florian Kugler's third example from this article.

Florian Kugler's investigation shows how this Core Data Stack is the faster one.

FetchedResultsController

Core data make sense to use with an FRC. The project comes with the basic FRC implementation for a table and collection view.

Accessing Core Data

TODO: The project will have a full suite to access the objects.

Installation

Podfile

platform :ios, '8.0'
pod 'CoreDataFullStack', '~> 0.4'

Then, run the following command:

$ pod install

Old school

Drag into your project the folder /CoreDataFullStack-iOS. That's all.

How to

General rule

To modify the database (insert new object, delete, update) access via the background backgroundManagedObjectContext.

To retrieve data to be shown in the UI access via the MainThread managedObjectContext

IMPORTANT

To avoid crossing threads use performBlockAndWait:^ with the backgroundManagedObjectContext

	[[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext performBlockAndWait:^
    {
    	// Insert
    	// Delete
    	// Update
    }

Go to Edit Scheme.../ Arguments / Arguments Passed On Launch and add: -com.apple.CoreData.ConcurrencyDebug 1

This line will tell you if you are crossing threads in your app.

To avoid problems be totally sure that your app is not crossing threads.

Stack Set up

//  AppDelegate.m

#import "CoreDataFullStack.h"

@interface AppDelegate () <CDFCoreDataManagerDelegate>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [CDFCoreDataManager sharedInstance].delegate = self;
    
    return YES;
}

#pragma mark - CDFCoreDataManagerDelegate

- (NSString *)coreDataModelName
{
    return @"ModelName";
}

Insert

Use CoreData system. It is really easy.

[[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext performBlockAndWait:^
    {
        CDFHouse *firstHouse = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([CDFHouse class])
                                                             inManagedObjectContext:[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext];
        
        firstHouse.houseID = @"0";
        firstHouse.town = @"London";
        
        CDFPerson *firstPerson = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([CDFPerson class])
                                                               inManagedObjectContext:[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext];
        
        firstPerson.personID = @"0";
        firstPerson.name = @"John";
        firstPerson.house = firstHouse;

        [[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext save:nil];
    }];

Retrieve

    NSArray *entries = [CDFRetrievalService retrieveEntriesForEntityClass:[CDFHouse class] managedObjectContext:[CDFCoreDataManager sharedInstance].managedObjectContext];

Delete

    [[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext performBlockAndWait:^
     {
         [CDFDeletionService deleteEntriesForEntityClass:[CDFHouse class]
                                       saveAfterDeletion:YES
                                    managedObjectContext:[CDFCoreDataManager sharedInstance].backgroundManagedObjectContext];
     }];

Count

	NSArray *entries = [CDFRetrievalService retrieveEntriesForEntityClass:[CDFHouse class]
                                                     managedObjectContext:[CDFCoreDataManager sharedInstance].managedObjectContext];

License

CoreDataFullStack-iOS is released under the MIT license. Please see the file called LICENSE.

Versions

$ git tag -a 0.4.0 -m 'Version 0.4.0'

$ git push --tags

Author

Gabriel Massana

##Found an issue?

Please open a new Issue here if you run into a problem specific to CoreDataFullStack-iOS, have a feature request, or want to share a comment.

About

A static library project that simplifies Core Data.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 99.0%
  • Ruby 1.0%