DCCoreDataManager is a Objective C library for iOS. It encapsulates all the Core Data Stack and give to the developeras a Thread Safe mode to use Core Data in your applications.
You need to add the CoreData Framework to your project if you wanto to use this pod.
DCCoreDataManager is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "DCCoreDataManager"
In your AppDelegate import the DCCoreDataManager.h file and init the instance.
#import "AppDelegate.h"
#import "CoreDataManager.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[DCCoreDataManager sharedInstance]setDataBaseName:@"Your_App_Model_Name"];
[[DCCoreDataManager sharedInstance]initContext];
return YES;
}
@end
Now you are ready to use Core Data on your app.
To save or create a new entity you need to pass a dictionary with the keys and values that you want to add. The keys must be the same as the attributes names of your entity.
NSMutableDictionary *entityValues = [NSMutableDictionary dictionary];
[entityValues setObject:@"A_User_Name" forKey:@"userName"];
[entityValues setObject:@"A_Name" forKey:@"Name"];
[entityValues setObject:@"A_Name_Last_Nam" forKey:@"lastName"];
User *entity = [[DCCoreDataManager sharedInstance]saveEntity:@"User" withValues:entityValues];
[[DCCoreDataManager sharedInstance] saveContext];
The saveEntity:withValues: method returns the saved Entity. You can used to manually add the possible relationships.
To retrieve entities, you have two options, retrieve all the entities withot distinction or pass a predicate to filter your results.
NSArray *results = [[DCCoreDataManager sharedInstance] getEntities:@"User"];
With the funciton getEntities: all the NSManagedObjects are going to be on a NSArray.
If you want you can set a predicate to filter your results.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"Name", @"Doe"];
NSArray *results = [[DCCoreDataManager sharedInstance] getEntities:@"User" withPredicate:predicate];
DCCoreDataManager gives you the posibility to update entities. To do this you have to pass the entity to be updated with the values dictionary.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"Name", @"Doe"];
NSArray *results = [[DCCoreDataManager sharedInstance] getEntities:@"User" withPredicate:predicate];
User *userEntity = [results objectAtIndex:0];
NSMutableDictionary *entityValues = [NSMutableDictionary dictionary];
[entityValues setObject:@"planet23" forKey:@"userName"];
userEntity = [[DCCoreDataManager sharedInstance]updateEntity:userEntity withValues:entityValues];
[[DCCoreDataManager sharedInstance] saveContext];
To delete a entity you only have to pass the entity to be deleted to the method deleteEntity:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K == %@", @"Name", @"Doe"];
NSArray *results = [[DCCoreDataManager sharedInstance] getEntities:@"User" withPredicate:predicate];
User *userEntity = [results objectAtIndex:0];
[[DCCoreDataManager sharedInstance] deleteEntity:userEntity];
[[DCCoreDataManager sharedInstance] saveContext];
David Cortes, @dcortes22
DCCoreDataManager is available under the MIT license. See the LICENSE file for more info.