Use Of--core data for iOS development

Source: Internet
Author: User
Tags uikit
<span id="Label3"></p><p style="text-align: center;"><p style="text-align: center;"><span style="font-size: 18pt;">Core Data Fundamentals</span></p></p><p><p>The official argument is that Core Data is a Schema-driven object graph management and persistence Framework.</p></p><p><p>The meaning of the translation is probably: Core data is a Schema-driven object graph management and persistence Framework.</p></p><p><p>well, the above literal meaning is not very easy to understand, then we can help those programmers with the rest of the development experience to set some ideas from the following aspects:</p></p><p><p></p></p> <ol> <ol> <li>Core data is not a database, but he may use a database. By default, Core Data uses sqlite, but you can do nothing about relational database-related knowledge, or SQL statements;</li> <li>Core data is not a hibernate-like object-relational mapping Framework. Core data is only a relational model between objects, users do not need database knowledge, and the data is not necessarily stored in the database;</li> <li>Core data allows you to save model objects to a file and retrieve the data as needed, which is similar to packaging and serialization operations, but core data can do more;</li> </ol> </ol><p><p></p></p><p><p>Some of the more powerful features that Core data can do include:</p></p><p><p></p></p> <ol> <ol> <li>Core data provides the basic ability to maintain changes in model objects so that you can perform undo and redo operations, and core data can maintain relationships between objects;</li> <li>Core data allows you to keep a subset of the model objects in memory for a given time, which is important for the harsh iOS memory environment;</li> <li>Use schema to describe model Objects. You can define the main features of your model in a graphical editor, and can even include relationships between individual objects. This provides a simple means of ensuring a basic robustness, such as the ability to set default values and attribute values for validation;</li> <li>Allows you to maintain objects that do not want to cross the Collection. For example, allows you to edit an object in one view, but this content may be discarded, causing the object to not be updated in another view;</li> <li>Provides support for versioning operations on objects, which makes it easy to upgrade legacy objects to new versions of objects;</li> </ol> </ol><p><p></p></p><p><p>Core data is not a separate class, but a collection of classes that need to collaborate with each other. The architecture diagram for the entire core data is more complex:</p></p><p><p></p></p><p><p>Instance</p></p><p><p>Mycoredata</p></p><p><p></p></p> <ol> <ol> <li><li>Create a new empty project, named mycoredata, Project attributes Select Core Data and Arc support;</li></li> <li><li>Before proceeding to the next step, we know a few things:<br><br> <ul> <li>The Managed Object Model describes the schema that you will use in the app. If you have database background knowledge, You can imagine the database Schema. The schema here actually refers to a collection of objects (or entities). In xcode, the model is saved in A. Xcdatamodeld File. You can use a visual editor to define entities, attributes, relationships, and so on;</li> <li>Persistent store coordinator, which uses SQLite to store persistent data by default in ios, and of course core data provides a means of storing different entities to different storage Objects. The PSC is the manager that maintains the means of Storage. Of course you can completely forget this object, because you basically do not need to interact directly with him;</li> <li>Managed Object Context, which is used to manage the serialization and deserialization of objects, and is the main tool used when using the core Data stack;</li> <li>You can see from the bottom up that the order Is: datastore->persistent object Store->persistent Store coordinator->managed Object context->app;</li> </ul></li></li> <li><li>The basic concept is finished, we click on the project generated by The. Xcdatamodel file, Click Add entity, Add a body, named novel;<br><br><br></li></li> <li><li>To add a property, define the following three property values:<br><br><br></li></li> <li><li>Create a user interface, create a storyboard in file->new, and set it to this story Board in the project properties;</li></li> <li>In the APPDELEGATE.M file, the implementation of the Auto-generated Didfinishlaunchingwithoptions method is emptied and becomes:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_1" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_1" width="up" height="align=" "middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "> <param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param Name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value=" Http://www.macromedia.com/go/getflashplayer "><param name=" flashvars " value=" id=1&width=18&height=18 ">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">-(<span class="datatypes">BOOL) application: (uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions </span></li> <li>{</li> <li class="alt"><span class="keyword">return YES; </span></li> <li>}</li> </ol> </ol></li> <li><li>In the story board editing interface, add a table view controller, A view controller, set the table view controller to jump to the view controller ( Note Select the modal for segue, since the Details window is released using Dismissviewcontroller later, and embed in to navigator View Controller;</li></li> <li><li>Add a button to the table View controller and select the Button's identifier as add to make it a plus button;</li></li> <li><li>Select the cell of Table view and change its style to right Detail;</li></li> <li><li>To the view Controller named details, first place a navigation bar to the top of the interface, then drag the two buttons above the navigation bar and set the corresponding bar Button respectively. The identifier property for item is cancel and save, as Follows:<br></li></li> <li><li>In the details interface, add three text field, and set their placeholder property as: name, author, version;</li></li> <li><li>The plus button above the ListView needs to jump to the details interface when clicked, so you need to select this button on story board, hold down the control key, drag to the Details view and release the mouse, select Segue-> Modal;</li></li> <li><li>In the source code folder to create a object-c class, named novellistviewcontroller, the parent class select uitableviewcontroller;</li></li> <li><li>In story board, the class of Table view is set to novellistviewcontroller;<br></li></li> <li><li>similarly, set the details of this view as the new noveldetailsviewcontroller;</li></li> <li>In noveldetailsviewcontroller, the corresponding control variables and response methods are generated and TextField in the Interface:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_2" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="zeroclipboardmovie_2" width="up" height="align=" "middle" bgcolor="#ffffff" data="/http static.blog.csdn.net/scripts/zeroclipboard/zeroclipboard.swf " type=" Application/x-shockwave-flash "><param name=" SRC " value=" http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf "> <param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param Name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value=" Http://www.macromedia.com/go/getflashplayer "><param name=" flashvars " value=" id=2&width=18&height=18 ">< param name= "wmode" value= "transparent" > </object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><span class="preprocessor">#import <UIKit/UIKit.h></span></li> <li></li> <li class="alt">@interface Noveldetailsviewcontroller:uiviewcontroller</li> <li>@property (strong, nonatomic) iboutlet Uitextfield *tfname;</li> <li class="alt">@property (strong, nonatomic) iboutlet Uitextfield *tfauthor;</li> <li>@property (strong, nonatomic) iboutlet Uitextfield *tfversion;</li> <li class="alt"></li> <li>-(ibaction) cancel: (id) sender;</li> <li class="alt">-(ibaction) save: (id) sender;</li> <li>@end</li> </ol> </ol></li> <li><li>next, we'll move on to the core data phase. The main features we will implement include: saving data in the details page, retrieving the data in the list page;</li></li> <li>Add code to Noveldetailsviewcontroller.m:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_3" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="flashvars" value="id=3&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt">-(nsmanagedobjectcontext *) Managedobjectcontext {</li> <li>Nsmanagedobjectcontext *context = nil;</li> <li class="alt">ID delegate = [[uiapplication sharedapplication] delegate];</li> <li><span class="keyword">if ([delegate performselector: @selector (managedobjectcontext)]) {</span></li> <li class="alt">context = [delegate managedobjectcontext];</li> <li>}</li> <li class="alt"><span class="keyword">return context; </span></li> <li>}</li> </ol> </ol></li> <li><li>Remember when we added core data support to the project, we automatically added a managed object context to our appdelegate, and we will use this context to save or retrieve the object;</li></li> <li>We changed the event handling method of the two buttons to:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_4" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_4" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="flashvars" value="id=4&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(ibaction) cancel: (id) Sender {</li></li> <li><li>[self Dismissviewcontrolleranimated:yes completion:nil];</li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">-(ibaction) save: (id) Sender {</li></li> <li><li><span class="comment">//get Managed Object Context</span></li></li> <li class="alt"><li class="alt">Nsmanagedobjectcontext *context = [self managedobjectcontext];</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">//Create A new managed object</span></li></li> <li><li>Nsmanagedobject *newnovel = [nsentitydescription insertnewobjectforentityforname:@<span class="string">"Novel"  inmanagedobjectcontext:context]; </span></li></li> <li class="alt"><li class="alt">[newnovel setValue:self.tfName.text forkey:@<span class="string">"name"]; </span></li></li> <li><li>[newnovel setValue:self.tfAuthor.text forkey:@<span class="string">"author"]; </span></li></li> <li class="alt"><li class="alt">[newnovel setValue:self.tfVersion.text forkey:@<span class="string">"version"]; </span></li></li> <li><li></li></li> <li class="alt"><li class="alt">Nserror *error = nil;</li></li> <li><li><span class="comment">//Save the object to persistent store</span></li></li> <li class="alt"><li class="alt"><span class="keyword">if (![ Context Save:&error]) {</span></li></li> <li><li>NSLog (@<span class="string">"Can ' t save!  %@%@ ", error, [error localizeddescription]); </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">[self Dismissviewcontrolleranimated:yes completion:nil];</li></li> <li><li>}</li></li> </ol>When you click the Cancel button, we use an animated method to close the details WINDOW.<br>When the Save button is clicked, we first get the context, then create a new managed Object, set the properties of the novel, then call the context save to store the data, and finally close the Interface.</li> <li>Now let's see how to get the List. We add an array in Tableviewcontroller to hold the list of novel:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_5" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_5" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="flashvars" value="id=5&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <ol class="dp-cpp" start="1"> <li class="alt"><span class="preprocessor">#import <UIKit/UIKit.h></span></li> <li></li> <li class="alt">@interface Novellistviewcontroller:uitableviewcontroller</li> <li></li> <li class="alt">@property (strong, Nonatomic) Nsmutablearray *novellist;</li> <li></li> <li class="alt">@end</li> </ol> </ol></li> <li>Also for the list page you need to add the get context and load time to get the array elements of the interface:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_6" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_6" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="flashvars" value="id=6&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">@implementation Novellistviewcontroller</li></li> <li><li></li></li> <li class="alt"><li class="alt">@synthesize novellist;</li></li> <li><li></li></li> <li class="alt"><li class="alt">-(nsmanagedobjectcontext *) Managedobjectcontext</li></li> <li><li>{</li></li> <li class="alt"><li class="alt">Nsmanagedobjectcontext *context = nil;</li></li> <li><li>ID delegate = [[uiapplication sharedapplication] delegate];</li></li> <li class="alt"><li class="alt"><span class="keyword">if ([delegate performselector: @selector (managedobjectcontext)]) {</span></li></li> <li><li>context = [delegate managedobjectcontext];</li></li> <li class="alt"><li class="alt">}</li></li> <li><li><span class="keyword">return context; </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">-(<span class="keyword">void) viewdidappear: (<span class="datatypes">BOOL) animated</span> </span></li></li> <li><li>{</li></li> <li class="alt"><li class="alt">[super viewdidappear:animated];</li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="comment">//Fetch The devices from persistent data store</span></li></li> <li><li>Nsmanagedobjectcontext *managedobjectcontext = [self managedobjectcontext];</li></li> <li class="alt"><li class="alt">Nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@<span class="string">"novel"]; </span></li></li> <li><li>Self.novellist = [[managedobjectcontext executefetchrequest:fetchrequest error:nil] mutableCopy];</li></li> <li class="alt"><li class="alt"></li></li> <li><li>[self.tableview reloaddata];</li></li> <li class="alt"><li class="alt">}</li></li> </ol></li> <li>next, the table display module of the list interface is modified:<br><strong><strong>[cpp]</strong></strong>View Plaincopy<object id="ZeroClipboardMovie_7" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><object id="ZeroClipboardMovie_7" width="18" height="18" align="middle" bgcolor="#ffffff" data="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf" type="application/x-shockwave-flash"><param name="src" value="http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf"><param name="loop" value="loop"><param name="menu" value="false"><param name="quality" value="best"><param name="allowscriptaccess" value="always"><param name="allowfullscreen" value="false"><param name="pluginspage" value="http://www.macromedia.com/go/getflashplayer"><param name="flashvars" value="id=7&width=18&height=18"><param name="wmode" value="transparent"></object></object> <ol class="dp-cpp" start="1"> <li class="alt"><li class="alt">-(nsinteger) numberofsectionsintableview: (uitableview *) TableView</li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="preprocessor">#warning potentially incomplete method Implementation.</span></li></li> <li><li><span class="comment">//Return the number of Sections. </span></li></li> <li class="alt"><li class="alt"><span class="keyword">return 1; </span></li></li> <li><li>}</li></li> <li class="alt"><li class="alt"></li></li> <li><li>-(nsinteger) tableView: (uitableview *) tableView numberofrowsinsection: (nsinteger) section</li></li> <li class="alt"><li class="alt">{</li></li> <li><li><span class="preprocessor">#warning Incomplete Method Implementation.</span></li></li> <li class="alt"><li class="alt">//Return the number of the rows in the section <span class="comment">. </span></li></li> <li><li><span class="keyword">return self.novelList.count; </span></li></li> <li class="alt"><li class="alt">}</li></li> <li><li></li></li> <li class="alt"><li class="alt">-(uitableviewcell *) tableView: (uitableview *) tableView cellforrowatindexpath: (nsindexpath *) Indexpath</li></li> <li><li>{</li></li> <li class="alt"><li class="alt"><span class="keyword">static NSString *cellidentifier = @<span class="string">"Cell"; </span></span></li></li> <li><li>UITableViewCell *cell = [tableView dequeuereusablecellwithidentifier:cellidentifier forindexpath:indexpath];</li></li> <li class="alt"><li class="alt"></li></li> <li><li><span class="comment">//Configure the cell</span> ...</li></li> <li class="alt"><li class="alt">Nsmanagedobject *device = [self.novellist objectAtIndex:indexPath.row];</li></li> <li><li>[cell.textlabel settext:[nsstring stringwithformat:@<span class="string">"%@%@", [device valueforkey:@<span class="string">"name"], [device  valueforkey:@<span class="string">"version"]]; </span></span></span></li></li> <li class="alt"><li class="alt">[cell.detailtextlabel settext:[device valueforkey:@<span class="string">"author"]; </span></li></li> <li><li></li></li> <li class="alt"><li class="alt"><span class="keyword">return cell; </span></li></li> <li><li>}</li></li> </ol>Note that the Identifier property value of the table View cell is set to cell in story board, consistent with the Code.</li> <li><li>At this point, the program function is complete, see the discovery data can be saved normally, but we do not write a line and database-related Code.</li></li> </ol> </ol><p><p>Use Of--core data for iOS development</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.