Sunday, June 28, 2015

iOS Tutorial - Part 32 - Core Data I

Core Data I


Video Description 
In iOS we have two options for adding data into database. First option is Core Data and second option is SQLite. Since Core Data is more powerful and could be used in more object oriented way, so we will only talk about the first option, which is Core Data.

Add Entity, Add Attributes

If you check mark core data when you create the application, you would see a file with suffix "xcdatamodeld". If you click on it you would see
 
You can press plus button at the bottom of the page to add a new entity. Then you can add attributes by pressing add button under the attributes section. Each attribute needs a type. Here in the above screenshot all attributes are type string. Once we add all of the entities and attributes we want, we should create NSManagedObject subclass in order to have more object oriented programming style. To create NSManagedObject subclass, click on the file with suffix "xcdatamodeld" on your project structure and the from the tool menu, select Editor/Create NSManagedObject Subclass... .
Presee Next and then select the entity you want to create the subclass and then hit Create button to create your NSManagedObject class. If you open the created files you would see all of the attributes that you added for that entity. we can go ahead and add our a method in .m file for adding objects into database but it's not the best practice.

Category Files

Because later on the we may add more attribute into the application and if we auto generate the NSManagedObject superclass we would lose our functions, so it's better to add a Category class for this subclass. In order to create a category file, choose File / New / File ... Then select Objective-C File.
In the next screen give your file a name, and select a File Type as Category and select your entity name for the class field. Finally hit Create to create the Category file.

Add objects into database

Now we can add our method for adding objects into database. Here is the method we can create for adding objects:
+ (Students *)addStudentInfoFromDictionary:(NSDictionary *)studentInfo
{
    AppDelegate *appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    
    Students *studentEntity = nil;
    
    //Create a new Object
    studentEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Students" inManagedObjectContext:context];
    studentEntity.studentId = [studentInfo valueForKey:@"studentId"];
    studentEntity.name  = [studentInfo valueForKey:@"name"];
    studentEntity.lastName = [studentInfo valueForKey:@"lastName"];
    
    return studentEntity;
}
First two lines gets the context. Before using these two lines make sure you #import "AppDelegate.h" file. Next we create the instance of Students entity. We initialize it with the method insertNewObjectForEntityForName. Finally we can use entity properties to add our object value. Since we have this class in .m file we need to declare it in .h file so other classes can use it. So we should add the following line in the header file:
+ (Students *)addStudentInfoFromDictionary:(NSDictionary *)studentInfo;
Now we can use this method in any class we want. In this project we use it in our ViewController like bellow:
- (IBAction)addToDatabase:(UIButton *)sender
{
    NSDictionary *studentInfo = @{@"name": self.nameField.text,
                                  @"lastName": self.lastnameField.text,
                                  @"studentId": self.studentIdField.text};
    
    [Students addStudentInfoFromDictionary:studentInfo];
} 
Basically we create a dictionary with all the items we want to add into the database and then we pass it to the function that we creates in Student+Add class.

description property

Most Objective-C classes have a property called description. When you use this property it tries to get the description of that object. It's like toString method in other programming languages. We can use this property to get some output on the screen when we add an object into database. So we change the last line into the following line:
self.outPutTextView.text = [Students addStudentInfoFromDictionary:studentInfo].description; 

Download

Download this App from HERE.