CoreData is a way of data storage, CoreData is essentially the encapsulation of SQLite. CoreData is a powerful framework in the iOS SDK that allows programmers to store and manage data in an object-oriented manner. Using the CoreData framework, programmers can easily and efficiently manage data through an object-oriented interface. The CoreData framework provides object-relational mapping (ORM) capabilities that can convert OC objects to data stored in SQLite3 database files and restore data stored in the database to OC objects. In real development, you’ll see the Use Core Data option when you create a new project.

The biggest difference between CoreData and SQLite is that SQLite uses SQL statements and is procedural oriented. CoreData does not apply to SQL statements and encapsulates this process as object-oriented. Encapsulate tables, fields and records into entities, attributes and objects respectively, which is more in line with the idea of object orientation. Back in the project, after creating a new project, you will find that the CoreData project has an additional file with the suffix xcDatamodeld compared to the normal project

There will be three more properties and two more methods in the appdelegate. h file. The CoreData framework is also automatically imported

In the appdelegate. m file there will be a few more properties of the get method and the method implementation (lines 47 -125)

To add an entity click on the xcDatamodeld file -> AddEntity-> Double click on the entity name above and enter the desired entity name. (Note: Entity names must be capitalized)

Click the + sign under Attributes on the right to add Attributes

Adding objects Before we add objects, let’s take a look at the main objects in CoreData. Ns-managed ObjectContext: also called managed object context, which is responsible for the interaction between the application and the database (CRUD), i.e. used to store things. NSPersistentStoreCoordinator: persistent store coordinator. Add persistent repositories (such as SQLite databases) that bridge the connection between physical files and programs in the physical data store and manage different object contexts. Ns-managed Object Models used to store contexts are managed object models corresponding to defined model files. Attributes (fields) of managed objects. NSEntityDescription: EntityDescription (Always create an entity description before filling in the database. You can view it as something that specifies the table name.

/** */ @property()readonly,strong,nonatomic)NSManagedObjectContext*managedObjectContext; /** * The managed object model file * this object is actually an association between the XCDatamodel file and this object. Convert to object */ @property(readonly,strong,nonatomic)NSManagedObjectModel*managedObjectModel; /** * Persistent storage coordinator * is actually a bridge between the application and the database */ @property(readonly,strong,nonatomic)NSPersistentStoreCoordinator*persistentStoreCoordinator; * When saved, the content of the managed object context is saved through the persistent storage coordinator and is stored in a model file. */ - (void)saveContext; /** * the application documentation directory, which is the directory where CoreData holds the files ** @returnFile path * / - (NSURL *) applicationDocumentsDirectory; - (void)viewDidLoad {[superviewDidLoad]; / / used CoreData to add data AppDelegate * AppDelegate = [UIApplicationsharedApplication]. Delegate; self.manageObjectContext= [appDelegatemanagedObjectContext]; [self insertData]; [self updateData]; } - (void)insertData { NSEntityDescription*entity = [NSEntityDescriptionentityForName:@"Person"inManagedObjectContext:self.manageObjectContext];
Person*person = [[Personalloc]initWithEntity:entityinsertIntoManagedObjectContext:self.manageObjectContext];
person.name=@"Iron Man";
person.age=@40;
person.height=@180;
__autoreleasingNSError*error;
[self.manageObjectContextsave:&error];
if(error) {
NSLog(@"Failed to add data");
}else{
NSLog(@"Data added successfully"); }} Methods to modify data: To modify data, you only need to directly modify the attributes of the object, and then save the context. Add @Property (nonatomic,strong)Person*selectedPerson to the property list; SelectedPerson = person; selectedPerson= selectedPerson; - (void)updateData{// Modify data self.selectedPerson.name=@"Mark47"; self.selectedPerson.age=@1; __autoreleasingNSError*error; / / save data [self manageObjectContextsave: & error];if(error) {
NSLog(@"Data update failed");
}else{
NSLog(@"Data updated successfully"); }} Delete data: Delete the data as long as the executive deleteObject method, and then in the preservation context - (void) deleteData {[self. ManageObjectContextdeleteObject: self. SelectedPerson]; __autoreleasingNSError*error; [self.manageObjectContextsave:&error];if(error) {
      NSLog(@"Data deletion failed");
  }else{
      NSLog(@"Data deleted successfully"); }}Copy the code

To be continued…