CORE DATA:
Important KeyWord :NSManagedObjectContext,PersistentStoreCoordinator,PersistentObjectStore
important :database name is:Contacts
Attributes are :name,age,dob,address,sex
1.This Method used to get all values form database
-(NSManagedObjectContext *)managedObjectContext
{
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if([delegate performSelector:@selector(managedObjectContext)])
{
context = [delegate managedObjectContext];
}
return context;
}
2.Do U want Fetch Data from database mean use this method
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]initWithEntityName:@"Contacts"];
NSSortDescriptor *sortarray = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES comparator:nil];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortarray, nil]];
// NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"dhaya"];
// [fetchRequest setPredicate:predicate];
self.tableArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
3.Do u want insert value from database mean,use this method
NSManagedObjectContext *context = [self managedObjectContext];
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Contacts" inManagedObjectContext:context];
[newDevice setValue:_nameField.text forKey:@"name"];
[newDevice setValue:_ageField.text forKey:@"age"];
[newDevice setValue:_dobField.text forKey:@"dob"];
[newDevice setValue:_addressField.text forKey:@"address"];
[newDevice setValue:_sexField.text forKey:@"sex"];
NSData *data = UIImagePNGRepresentation(_imageView.image);
[newDevice setValue:data forKey:@"photo"];
NSError *error = nil;
if(![context save:&error])
{
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
4.Do u want Delete value from database mean use this method
NSManagedObjectContext *context = [self managedObjectContext];
if (editingStyle == UITableViewCellEditingStyleDelete) {
[context deleteObject:[self.tableArray objectAtIndex:indexPath.row]];
NSError *error = nil;
if(![context save:&error])
{
NSLog(@"Can't delete");
return;
}
[self.tableArray removeObjectAtIndex:indexPath.row];
5.Do u want update value from database mean use this method
[detailArray setValue:nameLabel.text forKey:@"name"];
6.Do u want sort from database mean use this method
NSSortDescriptor *sortarray = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES comparator:nil];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortarray, nil]];
7.Do u want filter the OutPut from database mean use this method
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name==%@",@"dhaya"];
[fetchRequest setPredicate:predicate];
8.enjoy coding
0 comments:
Post a Comment