Custom UITable. View. Cell with Dynamic Height .
Introduction to Core Data for i. OS and i. Phone Programming. Editor’s note: After we published the tutorial about saving data in plist file, some readers asked about Core Data and how we can use it to save persistent information. This week, we work with Ziad Tamim, an independent i. OS developer, to give you an introduction of Core Data and work with you to build a sample app using Core Data. This tutorial talks about persistence on i.
Phone (or other i. OS devices). What I mean by persistence is to make data that’s in your apps stay around between application launches. Persistence lets users store persistent data and also retrieve it, so that users don’t have to reenter all their data each time they use their applications.
There are multiple ways to store data in i. OS devices but most of them aren’t good enough to store a complicated data. They are usually used to save settings or to preload some data such as “Property List” and “Archiving Objects”.
So that’s why we’ll go through Core Data to see how you can utilize it to manage data in database. The focus of the tutorial is to provide a practical introduction of Core Data framework. I expect you’ve already gone through our tutorials about Storyboard and UITable. View. I will not give in- depth explanation about how to create view controller in Storyboard but you can always refer to the earlier tutorials to gain better understanding. Core Data is not a Database. When we talk about persistent data, people probably think of database. If you are familiar with Oracle or My.
Teamtreehouse – Build a Diary App Using Core Data. The course teaches you how to build a Diary App using Core Data.
SQL, you know that relational database stores data in the form of table, row and column, and it usually facilitates access through what- so- called SQL query. However, don’t mix up Core Data with database. Though SQLite database is the default persistent store for Core Data on i. Phone, Core Data is not a relational database. It is actually a framework that lets developers store (or retrieve) data in database in an object- oriented way.
With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL. To illustrate the concept, let’s begin and create your first app using Core Data.
This app is called My Store. It is a very simple app that stores all devices you have by collecting the name, version, company. My. Store App using Core Data. Creating a Sample App with Core Data. First let’s create a project with Core Data.
![Build A Diary App Using Core Data Build A Diary App Using Core Data](http://kayrules.com/wp-content/uploads/2014/06/create-newapp-e1401785618675.png)
Open Xcode and create a new Project, choose the template Empty Application as shown below. Create a New Project with Empty Application Template.
At the next screen, enter My. Store as a name of the project, select i. Phone in Devices family and don’t forget to select the options Use Storyboards, Use Core Data, Use Automatic Reference Counting.
Press next and create. Set up Xcode Project Options – Remember to select Use Core Data. Core Data Stack. Before we start working on the project, you first have to understand the Core Data Stack: Managed Object Model – It describes the schema that you use in the app.
![Build A Diary App Using Core Data Build A Diary App Using Core Data](http://officeblogswest.blob.core.windows.net/wp-content/migrated-images/53/0755.SharePointList.png)
If you have a database background, think of this as the database schema. However, 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 . 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 i. OS. 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. Forget about it you don’t understand what it is. You’ll not interact with Persistent Store Coordinator directly when using Core Data. Managed Object Context – Think of it as a “scratch pad” containing objects that interacts with data in persistent store. 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. The below illustration can probably give you a better idea about the Core Data Stack: Core Data Stack. Defining Managed Object Model.
This article highlights common mistakes developers make with Core Data. A common alternative to Core Data for persisting application data is to use XML Property. A simple journal application for the Mac, iPhone, and iPad. Day One is well designed and extremely focused to encourage you to write more.
Let’s move on to build the app. The first step is to open the Data Model named My. Store. xcdatamodeld and define the object model. Here we’ll define a Device entity that will be used to store the device information to database. To create an entity, click the + button. Click on the + button in the attributes section to do that. Add three attributes including name, version and company.
Set the type as String. Add 3 Attributes (company, name and version) to the Device entity.
The calorie counter app integrates with and shares data with more than 50. Learn how to build and manage powerful applications using Microsoft Azure cloud services. Safeguard access to your data and apps with an extra level of.
Designing the User Interface. Note: While we encourage you to build the user interface, you can also skip the procedures and download the project template from here. The template already comes with the Storyboard and set up all the view controller classes for you.
This gives you a good starting point to work on Core Data. If you use the template, you can skip this section and go directly to the “Diving Core Data” section.
![Build A Diary App Using Core Data Build A Diary App Using Core Data](http://www.appcoda.com/wp-content/uploads/2012/12/Screen-Shot-2012-12-07-at-5.16.49-PM-e1355053564257.png)
The next thing we need to do is to create the Storyboard that defines the views of our app. Navigate to File > New > New File and choose Storyboard in the User Interface template. Click next and select the i. Phone device family, click create. Creating the Storyboard. Once created, make sure to set the “Storyboard” you’ve just created as the main storyboard in the project setting.
Set the Storyboard you just created as the Main Storyboard. Also don. The method should be as simple as this. BOOL)application: (UIApplication *)application did. Finish. Launching. With. Options: (NSDictionary *)launch. Options. . Drag a button to the top- right part of navigation bar and set the identifier as “Add”. This will automatically change the button to a “+” button.
Next, select the prototype cell and change its style to “Right Detail”. Creating the Table View Controller. Drag a View Controller to the Storyboard and add a Navigation Bar to the top of the screen. Next, drag two buttons into the navigation bar.
Name one as “Cancel” and the other one as “Save”. In the content view, add three text fields and name the placeholder attributes as “Name”, “Version” and “Company”. This detail view will be shown when user taps the “+” button in the table view controller. So finally, press and hold the Control key, click the “+” button and drag towards the detail view controller. Select “Modal” as the Segue action to connect the table view controller and detail view controller. Designing the Detail View Controller.
Creating View Controller Classes. Create a new class by right- clicking on the My. Store folder > New File > Objective- C class, and name the class as Device. View. Controller. Make it as a subclass of UITable. View. Controller. Navigate to the Storyboard, select the Table View Controller and associate it with the Device.
View. Controller class. Set the Custom Class as Device. View. Controller. Once done, do the same steps to create a new class named Device. Detail. View. Controller. IView. Controller. Again, go to Storyboard and set the custom class of the detail view controller as the “Device.
Detail. View. Controller”. Lastly, wire up the UIText. Fields to the Device. Detail. View. Controller header file and create two action methods for the save and cancel buttons respectively. Creating IBOutlet and Action Methods. Your code should like this.
IBOutlet UIText. Field *name. Text. Field. @property (weak, nonatomic) IBOutlet UIText. Field *version. Text. Field. @property (weak, nonatomic) IBOutlet UIText. Field *company. Text. Field. - (IBAction)cancel: (id)sender. IBAction)save: (id)sender; @property(weak,nonatomic)IBOutlet.
UIText. Field*name. Text. Field; @property(weak,nonatomic)IBOutlet.
UIText. Field*version. Text. Field; @property(weak,nonatomic)IBOutlet. UIText. Field*company. Text. Field; -(IBAction)cancel: (id)sender; -(IBAction)save: (id)sender; Diving into Core Data. With the user interface, it’s time to go into the details of Core Data. Apparently, there are a couple of areas we have to implement: Save device information in the Detail View Controller. Fetch device information from persistent store (i.
SQLite database) and populate the data into Table View Controller. We’ll look into the implementation one by one. Saving Device Information. First, we need to implement the Device. Detail. View. Controller to let user add the devices to the database. Open up the Device.
Detail. View. Controller. Device. Detail. View. Controller. - (NSManaged. Object. Context *)managed. Object. Context . This method allows us to retrieve the managed object context from the App.
Delegate. Later we’ll use the context to save the device data. Next, we’ll implement the “save” and “cancel”, add the necessary code to look like this. IBAction)cancel: (id)sender !
Line 2 of the above code invokes the dismiss. View. Controller. Animated method to dismiss the current view controller with animation. For the “save” method, we first grab the managed object context. Every object that Core Data stores is inherited from NSManaged. Object. So we first create a new instance of NSManaged.
Object for the “Device” entity that we’ve defined in the object model. NSEntity. Description class provides a method named “insert. New. Object. For.
Entity. For. Name” for developer to create a managed object. Once you created the managed object (i. Lastly, we call up the “save: ” method of the context to save the object into database. You can now hit the Run button to try out your app.
Tap the “+” button to bring up the Detail View and save a new device. However, the new device is not yet displayed in the table. Let’s move on to see how you can fetch the device information from database. Fetching Device Information. Open Device. View.
Excellent Platforms for Building Mobile Apps. App. Makr is a DIY app creation platform that lets anyone craft i. OS, Android and HTML5 apps, with no coding knowledge required. It was founded in 2. Infinite Monkeys, to now form one of the largest DIY app publishing platforms worldwide.
You can make as many apps as you want, with unlimited updates, and utilize a number of features, including push notifications, high resolution photo galleries, live updates, music and video streaming, chat rooms, Google Maps integration, shared events calendars, in- app shopping, and much more. You keep track of all your apps through the Dashboard, easily customize your app's looks and functionality, add content with tabs, preview your app in real time and publish it to the markets with a single click. Price: Plans start at Free and go up to a $9.