Created
July 7, 2012 09:54
-
-
Save HHuckebein/3065690 to your computer and use it in GitHub Desktop.
Category on NSEntityDescription to set the correct persistent store
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
// | |
// NSEntityDescription+Toolbox.h | |
// | |
// | |
// Created by Rabe Bernd on 02.07.12. | |
// Copyright (c) 2012 RABE_IT Services. All rights reserved. | |
// | |
#import <CoreData/CoreData.h> | |
@interface NSEntityDescription (Toolbox) | |
+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context assignPersistentStoreSkipDefaultConfiguration:(BOOL)assign; | |
@end |
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
// | |
// NSEntityDescription+Toolbox.m | |
// | |
// | |
// Created by Rabe Bernd on 02.07.12. | |
// Copyright (c) 2012 RABE_IT Services. All rights reserved. | |
// | |
#import "NSEntityDescription+Toolbox.h" | |
#define kConfigurationDefault @"PF_DEFAULT_CONFIGURATION_NAME" | |
@implementation NSEntityDescription (Toolbox) | |
+ (id)insertNewObjectForEntityForName:(NSString *)entityName inManagedObjectContext:(NSManagedObjectContext *)context assignPersistentStoreSkipDefaultConfiguration:(BOOL)assign | |
{ | |
id object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context]; | |
if (assign && nil == [[object objectID] persistentStore]) { | |
NSString *entityName = [[object entity] name]; | |
NSPersistentStoreCoordinator *psc = context.persistentStoreCoordinator; | |
NSManagedObjectModel *mom = [psc managedObjectModel]; | |
NSPersistentStore *store = nil; | |
for (NSString *configuration in [mom configurations]) | |
{ | |
if (![configuration isEqualToString:kConfigurationDefault]) { | |
NSString *configName = configuration; | |
NSArray *objectNamesForConfiguration = [[mom entitiesForConfiguration:configuration] valueForKeyPath:@"name"]; | |
NSUInteger index = [objectNamesForConfiguration indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { | |
return [(NSString *)obj isEqualToString:entityName]; | |
}]; | |
if (index != NSNotFound) { | |
NSArray *persistentStores = [psc persistentStores]; | |
NSUInteger psIndex = [persistentStores indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) { | |
return [((NSPersistentStore *)obj).configurationName isEqualToString:configName]; | |
}]; | |
if (psIndex != NSNotFound) { | |
store = [persistentStores objectAtIndex:psIndex]; | |
[context assignObject:object toPersistentStore:store]; | |
} | |
} | |
} | |
} | |
} | |
return object; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment