Core data the basic snippets

Core data the basic snippets

where to buy priligy in india buy provigil online in india Let’s start
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
#import <CoreData/CoreData.h>

Creating a managedObjectContext, you will need a managedObjectContext with almost every Core data request you make.

[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
– (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate performSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}

Inserting a object to an entity

[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
NSManagedObjectContext *context = [self managedObjectContext];
// Create a new managed object
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@”Drankspellen” inManagedObjectContext:context];
[newDevice setValue:id forKey:@”id”];
[newDevice setValue:date forKey:@”date”];
[newDevice setValue:[NSNumber numberWithFloat:afloat] forKey:@”float”];
[newDevice setValue:category forKey:@”category”];

Getting all objects in an entity
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@”Drankspellen”];
Spellen = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];

for (NSDictionary *spel in Spellen) {
// [alleSpelIDs addObject:[spel objectForKey:@”id”]];
NSLog(@”%@”, [spel valueForKey:@”naam”]);
}

Setting a NSPredicate on the fetch
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
NSPredicate *predicateString = [NSPredicate predicateWithFormat:@”category contains[cd] %@”, myCategory];
[fetchRequest setPredicate:predicateString];

Setting a sortDescriptor on a fetch
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”datum”
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

Deleting all objects in an entity
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
– (void) deleteAllObjects: (NSString *) entityDescription {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:entityDescription inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

for (NSManagedObject *managedObject in items) {
[managedObjectContext deleteObject:managedObject];
NSLog(@”%@ object deleted”,entityDescription);
}
if (![managedObjectContext save:&error]) {
NSLog(@”Error deleting %@ – error:%@”,entityDescription,error);
}

}

Deleting a specific entry in CoreData
[cc lang=”objc” escaped=”true” width=”90%” theme=”default” line_numbers=”on”]
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@”EntityName”];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”propertyName == %@”, value];
[request setPredicate:predicate];

NSError *error = nil;
NSManagedObjectContext *managedObjectContext;//Get your ManagedObjectContext;
NSArray *result = [managedObjectContext executeFetchRequest:request error:&error];
for (NSManagedObject *product in result) {
[context deleteObject:product];
}