Saturday, 7 September 2013

CoreData Simple Tutorial

While searching for a simple solution in Core data Implementation I found this tutorial in you-tube
Its have four parts. Its explains how to start with an Empty application in x-code with core data ticked, how to create an Entity in core data , how to create attributes for that Entity and Put data into core data table and Delete data from table with a simple User interface

The Core Data model is made up of two components:
  • The managed object model: The managed object model describes the database schema (entities and their properties). 
  • The Core Data stack: The Core Data stack is made up of three parts (you can have more than one set of these): 
    • Persistent store: Think of this as the database itself (usually SQLite). 
    • Persistent store coordinator: Think of this as the “database connection”. Each coordinator can have one and only one managed object model and one persistent store (which it is initialized with). 
    • Managed object context: Think of this as the “scratch pad.” Each context can have one and only one persistent store coordinator (which it is instantiated with).



Part 4 - Deleting Data from Core-data


AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];
 /* Inserting Data */
    Friends *friend = [NSEntityDescription insertNewObjectForEntityForName:@"Friends" inManagedObjectContext:appDelegate.managedObjectContext];
    friend.name=@"John";
    friend.mobile=@9812345678;
    [appDelegate saveContext];
 /* Fetching Data */
    NSFetchRequest *fetchRequest =[NSFetchRequest fetchRequestWithEntityName:@"Friends"];
    NSArray *results = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:nil];
    for (Friends *friend in results) {
        NSLog(@"Name : %@  \n Mobile : %@",friend.name,friend.mobile);
    }
/* Deleting Data */
    Friends *fakeFriend = [results firstObject];
    [appDelegate.managedObjectContext deleteObject:fakeFriend];
    [appDelegate saveContext];

No comments:

Post a Comment