Skip to content

Instantly share code, notes, and snippets.

@pronebird
Created December 23, 2015 13:41
Show Gist options
  • Save pronebird/1a6db53e6f75e7baf109 to your computer and use it in GitHub Desktop.
Save pronebird/1a6db53e6f75e7baf109 to your computer and use it in GitHub Desktop.
MagicalRecord + Seam bridge
//
// 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