Monday, June 27, 2016

How to Start Coredata - Coredata Tutorial in Objective C


/*!
 Saving Data objects method
*/
-(void)savingDataIntoCoreData
{
    NSManagedObjectContext *context = [self managedObjectContext];
   
    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
    [newDevice setValue:@"iPhone 5" forKey:@"name"];
    [newDevice setValue:[NSNumber numberWithInt:9.0] forKey:@"version"];
    [newDevice setValue:@"rBA" forKey:@"company"];
   
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    [self fetchingDataFromCoreData];
}

/*!
Fetching Data from SQLITE using coredata
*/
-(void)fetchingDataFromCoreData
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
    NSArray * devicesArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSLog(@"Devices %@",devicesArray);
}

/*!
Getting  managedObjectContext object
*/
- (NSManagedObjectContext *)managedObjectContext {
   
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}


Deleting Object Using CoreData :

-(void)deleteObject:(NSManagedObject *)object
{
 NSManagedObjectContext *context = [self managedObjectContext];
[context deleteObject:object];
 NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }
 
}


CoreData stack :

Managed Object Model :
It describes the schema that you use in the app. The schema is represented by a collection of objects (also known as entities). In Xcode, the Managed Object Model is defined in a file with the extension .xcdatamodeld. You can use the visual editor to define the entities and their attributes, as well as, relationships.
Persistent Store Coordinator :
SQLite is the default persistent store in iOS. However, Core Data allows developers to setup multiple stores containing different entities. The Persistent Store Coordinator is the party responsible to manage different persistent object stores and save the objects to the stores.
Managed Object Context  :
Its job is to manage objects created and returned using Core Data. Among the components in the Core Data Stack, the Managed Object Context is the one you’ll work with for most of the time. In general, whenever you need to fetch and save objects in persistent store, the context is the first component you’ll talk to. 

Saving Objects using Core Data :

Every object that Core Data stores is inherited from NSManagedObject. So we first create a new instance of NSManagedObject for the “Device” entity that we’ve defined in the object model. NSEntityDescription class provides a method named “insertNewObjectForEntityForName” for developer to create a managed object. Once you created the managed object (i.e. newDevice), you can set the attributes (name, version, company) using the user input. Lastly, we call up the “save:” method of the context to save the object into database. 
Fetching Data using Core Data :
First we need to get the managed object context. To fetch the device info from the database   
we need to create instance of NSFetchRequest  and set the entity Device and invokes method executeFetchRequest to retirve all the data related to Device  entity. Similar to SELECT caluse in relataion database.

CoreData Tutorial with Objective C


/*!
 Saving Data objects method
*/
-(void)savingDataIntoCoreData
{
    NSManagedObjectContext *context = [self managedObjectContext];
   
    // Create a new managed object
    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context];
    [newDevice setValue:@"iPhone 5" forKey:@"name"];
    [newDevice setValue:[NSNumber numberWithInt:9.0] forKey:@"version"];
    [newDevice setValue:@"rBA" forKey:@"company"];
   
    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
    [self fetchingDataFromCoreData];
}

/*!
Fetching Data from SQLITE using coredata
*/
-(void)fetchingDataFromCoreData
{
    NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];
    NSArray * devicesArray = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSLog(@"Devices %@",devicesArray);
}

/*!
Getting  managedObjectContext object
*/
- (NSManagedObjectContext *)managedObjectContext {
   
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}


Deleting Object Using CoreData :

-(void)deleteObject:(NSManagedObject *)object
{
 NSManagedObjectContext *context = [self managedObjectContext];
[context deleteObject:object];
 NSError *error = nil;
        if (![context save:&error]) {
            NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]);
            return;
        }
 
}


CoreData stack :

Managed Object Model :
It describes the schema that you use in the app. The schema is represented by a collection of objects (also known as entities). In Xcode, the Managed Object Model is defined in a file with the extension .xcdatamodeld. You can use the visual editor to define the entities and their attributes, as well as, relationships.
Persistent Store Coordinator :
SQLite is the default persistent store in iOS. However, Core Data allows developers to setup multiple stores containing different entities. The Persistent Store Coordinator is the party responsible to manage different persistent object stores and save the objects to the stores.
Managed Object Context  :
Its job is to manage objects created and returned using Core Data. Among the components in the Core Data Stack, the Managed Object Context is the one you’ll work with for most of the time. In general, whenever you need to fetch and save objects in persistent store, the context is the first component you’ll talk to. 

Saving Objects using Core Data :

Every object that Core Data stores is inherited from NSManagedObject. So we first create a new instance of NSManagedObject for the “Device” entity that we’ve defined in the object model. NSEntityDescription class provides a method named “insertNewObjectForEntityForName” for developer to create a managed object. Once you created the managed object (i.e. newDevice), you can set the attributes (name, version, company) using the user input. Lastly, we call up the “save:” method of the context to save the object into database. 
Fetching Data using Core Data :
First we need to get the managed object context. To fetch the device info from the database   
we need to create instance of NSFetchRequest  and set the entity Device and invokes method executeFetchRequest to retirve all the data related to Device  entity. Similar to SELECT caluse in relataion database.