PFIncrementalStore

Drop-in Core Data + Parse Persistence

View project onGitHub

Welcome to PFIncrementalStore

This drop-in class manages persistence syncing between Core Data and Parse seamlessly. PFIncrementalStore is an NSIncrementalStore subclass that uses Parse to automatically request resources as properties and relationships are needed.

Installation

Requirements

PFIncrementalStore requires a subscription to Parse, a valid Parse App, API Key and Secret, and minor programming knowledge. Parse subscription and API information can be obtained at https://www.parse.com/

PFIncrementalStore requires Xcode 4.4 with the iOS 5.0 SDK, as well as Parse 1.2 or higher.

Cocoapods

CocoaPods is the recommended way to add PFIncrementalStore to your project.

Here's an example podfile that installs PFIncrementalStore and its dependency, Parse:

Podfile

platform :ios, '5.0'

pod 'PFIncrementalStore'

Note the specification of iOS 5.0 as the platform; leaving out the 5.0 will cause CocoaPods to fail with the following message: PFIncrementalStore is not compatible with iOS 4.3.

Create a PFIncrementalStore Subclass

#import <PFIncrementalStore/PFIncrementalStore.h>

@interface IncrementalStore : PFIncrementalStore

+ (NSURL *)modelURL;

@end
#import "IncrementalStore.h"

@implementation IncrementalStore

+ (void)initialize {
    [NSPersistentStoreCoordinator registerStoreClass:self forStoreType:[self type]];
}

+ (NSString *)type {
    return NSStringFromClass(self);
}

+ (NSURL *)modelURL {
    return [[NSBundle mainBundle] URLForResource:@"CoreDataModel" withExtension:@"momd"];
}

+ (NSManagedObjectModel *)model {
    return [[NSManagedObjectModel alloc] initWithContentsOfURL:[self modelURL]];
}

@end

Utilize the Subclass in your Persistent Store Coordinator

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
    if (__persistentStoreCoordinator == nil) {
        NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.sqlite",[self coreDataStack]]];

        NSError *error = nil;
        __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[IncrementalStore model]];

        IncrementalStore *incrementalStore = (IncrementalStore *)[__persistentStoreCoordinator addPersistentStoreWithType:[IncrementalStore type]
                                                                                                                configuration:nil URL:nil options:nil error:nil];

        NSDictionary *options = @{
                                  NSInferMappingModelAutomaticallyOption : @(YES),
                                  NSMigratePersistentStoresAutomaticallyOption: @(YES)
                                  };

        if (![incrementalStore.backingPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

            abort();
        }
    }
    return __persistentStoreCoordinator;
}

Make sure to initialize Parse with your Keys, possibly in your AppDelegate

[Parse setApplicationId:<# Parse Application ID #>
              clientKey:<# Parse Client Key #>];

Use Core Data like you normally would

PFIncrementalStore is designed to work in the background with no additional hassle! You're free to make fetch & save requests through Core Data and PFIncrementalStore will take care of syncing with Parse.

Features

PFFile Support

Create an attribute on your Parse Object with the PFFile type and create an associated NSData attribute on your Core Data model, it's as simple as that! PFIncrementalStore takes care of syncing the data in the background for you!

PFUser Support - Coming Soon

We're working on a way to sync PFUsers as Core Data objects, you can follow the progress here!

Credits

PFIncrementalStore was created by Scott BonAmi (@snb828).

Disclaimer

PFIncrementalStore is not affiliated, associated, authorized, endorsed by, or in any way officially connected with Parse.com, Parse Inc., or any of its subsidiaries or its affiliates. The official Parse web site is available at www.parse.com.