Core data is a framework that appears after IOS5, which provides object-relational mapping (ORM) functionality that enables you to convert OC objects into data, store them in SQLite database files, and restore data saved in a database to OC objects. During this data operation, we do not need to write any SQL statements
1. Select Use the CoreData when creating the project
2. Once created, you will see a Coredata.xcdatamodeldde file in the left filename
3. Click to see its contents in the middle of Xcode, then click on the bottom left corner of add entity can be added table (table name can be modified, but the first letter must be capitalized)
4. Add attributes to the table
5. Creating a data model for a table
(1) Click New File
(2) Click Next
(3) Click Next
(4) Which table to create the data model to tick which one, and then click Next
Read CoreData
1. Read Managedobjectmodel
2. Find the names of all the entities inside [model Entitiesbyname]
3. Find the entity to read nsentitydescription *entry = entrydic[@ "UserInfo"];
4, initialize the query object nsfetchrequest *request = [[Nsfetchrequest alloc]init];
5, through the context to find nsarray *list = [context executefetchrequest:request Error:nil];
1Nsmanagedobjectcontext *context =[[Appdelegate appdelegate] managedobjectcontext];2 3 4 5 //1. Read Managedobjectmodel6 7Nsmanagedobjectmodel *model =[[Appdelegate appdelegate] managedobjectmodel];8 9 //2. Find the names of all the entities inside [model Entitiesbyname]Ten OneNsdictionary *entitiesdic =[model Entitiesbyname]; A - //3. Find the entity to read nsentitydescription *entry = entrydic[@ "UserInfo"]; - theNsentitydescription *entity = entitiesdic[@"Userinfo"]; - - //4. Initialize Query Request Object Nsfetchrequest - +Nsfetchrequest *request =[[Nsfetchrequest alloc]init]; - +Request.entity =entity; A at //request.predicate - - //Request.fetchlimit - - //Request.fetchoffset - in - to //5. Start the query through the context + -Alist =[Context Executefetchrequest:request Error:nil]; the *NSLog (@"%@", alist); $ Panax Notoginseng - the if(alist.count!=0) { + A [Mytableview Reloaddata]; the + //[Mytableview reloadsections:[nsindexset indexsetwithindex:0] withrowanimation:uitableviewrowanimationnone];
- $ } $ - - the
Note: Appdelegate This method is defined in the Appdelegate file, in the. h file Declaration
+ (Appdelegate *) appdelegate;
In the. m file implementation
+ (Appdelegate *) appdelegate{ return (Appdelegate *) [uiapplication sharedapplication]. Delegate;}
Operation CoreData
1, regardless of additions and deletions need to first initialize the context [app Managedobjectcontext]
2. Insert specific content into context
+ (ID) insertnewobjectforentityforname: (NSString *) entityname inmanagedobjectcontext: (Nsmanagedobjectcontext *) Context
3. Save Savecontext
- (void) addentity{Nsmanagedobjectcontext* ConText =[[Appdelegate Appdelegate]managedobjectcontext]; Massage* massage = [nsentitydescription insertnewobjectforentityforname:@"Massage"Inmanagedobjectcontext:context]; [Massage setValue:postView.titleTextFild.text Forkey:@"title"]; [Massage setValue:postView.contentTextFild.text Forkey:@"content"]; Userinfo*userinfo = [Nsentitydescription insertnewobjectforentityforname:@"Userinfo"Inmanagedobjectcontext:context]; Nsarray* List = @[@"Chen 3.jpg",@"Chen 2.jpg",@"is 1.jpg",@"is 3.jpg"]; intArc = arc4random ()%List.count; UIImage*img =[UIImage Imagenamed:list[arc]]; [UserInfo SetValue:@"hehehe"Forkey:@"name"]; [UserInfo setvalue:@ ( One) Forkey:@" Age"]; [UserInfo setvalue:uiimagejpegrepresentation (IMG,1) Forkey:@"Headimage"]; [UserInfo setvalue:massage Forkey:@"Massage"]; [[Appdelegate appdelegate] savecontext]; }
IOS-CoreData of data persistence