IOS growth path-class used for Core Data-microsolution

Source: Internet
Author: User
Table Structure: NSEntityDescription is equivalent to a table in the database. TA describes an abstract data type: eg: // + insertNewObjectForEntityForName: inManagedObjectContext: Factory method, which is described according to the given Entity, generate an NSManagedObject object and insert it to ManagedObjectContext. Student * student = [NSEntityDescription insertNewObjectForEntityForName: @ "Student" inManagedObjectContext: managedObjectContext]; // use the code above to obtain the instance of the student table, and then assign the student value to the attribute in the table. name = @ "like"; student. age = 12; Application The data model of the program, the relationship between all tables in the database and them: NSManagedObjectModel // The system will read the model file to claim the NSManagedObjectModel object. model: For class, model is called entity; for instant variable, the model is called propertymodel and contains two types of property: attributes and relationships. attribute is a simple data type, such as a string, date, and number NSManagedObjectModel * model = [self managedObjectModel]; // gets the instance NSDictionary * entities = [model entitiesByName]; // entitiesByName: Obtain the names of all tables. NSEntityDescription * entity = [Entities valueForKey: @ "Student"]; // find the table database storage method named Student from it: NSPersistentStoreCoordinator // use the Core Data document type application, generally, data is read or stored from the data files on the disk. The underlying read/write operations are handled by the Persistent Store Coordinator. In general, we do not need to deal with it directly to read and write files. The Managed Object Context has done this part of database operations for us to call Persistent Store Coordinator: NSManagedObjectContext the managed object context (direct operations on data) // you can access the underlying framework object set through TA. These object sets are collectively referred to as persistence stacks) -- It provides an access channel between an application and an Object stored in external data. // Managed Object Context is very important. Operations on data objects are related to it. When a data Object is created and inserted into the Managed Object Context, the Managed Object Context starts to track all changes to the data Object and provides undo/redo support when appropriate, or call Persistent Store Coordinato to save the changes to the data file. // redo: Restore committed transactions. undo: rollback. read consistency is supported, failed transaction recovery //-executeFetchRequest: error: Execute the Fetch Request and return all matched data objects NSFetchRequest * fetch = [[NSFetchRequest alloc] init]; NSArray * results = [context executeFetchRequest: fetch error: nil]; // get the instance of NSManagedObjectContext // 1. obtain id delegate = [[UIApplication sharedApplication] delegate]; self. managedObjectContext = [delegate managedObjectContext]; // 2. directly create an instance NSManagedObjectContext * context = [self managedObjectContext]; // use the getter method to obtain the query statement: NSPetchRequest is equivalent to a select statement // Fetch Requests is equivalent to a query statement, you must specify the Entity to query. We use Fetch Requests to query Qualified Data Objects from the Managed Object Context and return the query results in the form of NSArray. If no query conditions are set, all data objects of the Entity are returned. You can use a predicate to set query conditions. Usually, the commonly used Fetch Requests are saved to a dictionary to reuse NSPredicate * predicate = [NSPredicate predicateWithFormat: @ "xx >% @", xx]; // equivalent to the query condition NSSortDescriptor * sort = [[NSortDescriptor alloc] initWithKey: @ "name"] in the select statement; // sort NSArray * sortDescriptors = [NSArray arrayWithObject: sort] by name; //-setEntity: sets the type of the data object to be queried (Entity) is the NSEntityDescription object //-setPredicate: sets the query condition //-setFetchLimit: sets the maximum number of query objects //-setSortDescriptors: sets the sorting method of query results //-setAffectedStores: set the Data storage in which table records can be queried: each object in NSManagedObject Core Data storage is inherited from NSManagedObject. We can get an instance of TA, use this instance to assign values to the attributes in the table. For example: NSManagedObject * managedObject = [NSEntityDescription insertNewObjectForEntityForName: @ "Student" inManagedObjectContext: managedObjectContext. name = @ "like"; managedObject. age = 12;

Related Article

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.