Created
December 23, 2015 13:41
-
-
Save pronebird/1a6db53e6f75e7baf109 to your computer and use it in GitHub Desktop.
MagicalRecord + Seam bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MagicalRecord + Seam bridge | |
// | |
#import "MagicalRecord+SeamStore.h" | |
@import Seam; | |
@implementation NSPersistentStoreCoordinator (SeamStore) | |
+ (NSPersistentStoreCoordinator *)MR_coordinatorWithAutoMigratingSeamStoreAtURL:(NSURL *)storeURL | |
{ | |
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel]; | |
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; | |
[[NSFileManager defaultManager] createDirectoryAtURL:[storeURL URLByDeletingLastPathComponent] | |
withIntermediateDirectories:YES | |
attributes:nil | |
error:nil]; | |
NSError *error; | |
SMStore *store = [coordinator addPersistentStoreWithType:[SMStore type] | |
configuration:nil | |
URL:storeURL | |
options:nil | |
error:&error]; | |
if(!store) { | |
[MagicalRecord handleErrors:error]; | |
abort(); | |
} | |
return coordinator; | |
} | |
@end | |
@implementation MagicalRecord (SeamStore) | |
+ (void)setupCoreDataStackWithSeamStoreAtURL:(NSURL *)storeURL | |
{ | |
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return; | |
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSeamStoreAtURL:storeURL]; | |
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator]; | |
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator]; | |
} | |
+ (void)setupCoreDataStackWithSeamStoreNamed:(NSString *)storeName | |
{ | |
NSURL *storeURL = [NSPersistentStore MR_urlForStoreName:storeName]; | |
[self setupCoreDataStackWithSeamStoreAtURL:storeURL]; | |
} | |
+ (void)setupCoreDataStackWithSeamStore | |
{ | |
[self setupCoreDataStackWithSeamStoreNamed:[self defaultStoreName]]; | |
} | |
+ (void)handleSeamStorePush:(NSDictionary *)notification { | |
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_defaultStoreCoordinator]; | |
for(NSPersistentStore *store in [coordinator persistentStores]) { | |
if([store isKindOfClass:[SMStore class]]) { | |
[((SMStore *)store) handlePushWithUserInfo:notification]; | |
} | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment