[IOS] Quick query of Core Data code, ioscore
The code in this article comes from: http://www.appcoda.com/introduction-to-core-data/
If you want to learn Core Data, do not miss it. The following are some common code snippets recorded by me.
There is an Entity: Device, which has three attributes: company, name, and version.
1. method for obtaining context:
- (NSManagedObjectContext *)managedObjectContext { NSManagedObjectContext *context = nil; id delegate = [[UIApplication sharedApplication] delegate]; if ([delegate performSelector:@selector(managedObjectContext)]) { context = [delegate managedObjectContext]; } return context;}
2. Add a data entry:
NSManagedObjectContext *context = [self managedObjectContext]; // Create a new managed object NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Device" inManagedObjectContext:context]; [newDevice setValue:self.nameTextField.text forKey:@"name"]; [newDevice setValue:self.versionTextField.text forKey:@"version"]; [newDevice setValue:self.companyTextField.text forKey:@"company"]; NSError *error = nil; // Save the object to persistent store if (![context save:&error]) { NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]); }
3. delete a piece of data:
NSManagedObjectContext *context = [self managedObjectContext];// Delete object from database[context deleteObject:[self.devices objectAtIndex:indexPath.row]];NSError *error = nil;if (![context save:&error]) { NSLog(@"Can't Delete! %@ %@", error, [error localizedDescription]); return;}
4. modify a piece of data:
NSManagedObjectContext *context = [self managedObjectContext]; // Update existing device[self.device setValue:self.nameTextField.text forKey:@"name"];[self.device setValue:self.versionTextField.text forKey:@"version"];[self.device setValue:self.companyTextField.text forKey:@"company"];NSError *error = nil;// Save the object to persistent storeif (![context save:&error]) { NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);}
5. query a bunch of data:
// Fetch the devices from persistent data storeNSManagedObjectContext *managedObjectContext = [self managedObjectContext];NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Device"];self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
Coredata code for IOS development
Read the data in coredata and display it. it's almost the same as querying the table in the database. Go to cocachina and devdiv to see it and I will soon understand it.
How to access coredata in iphone Development
Coredata uses sqlite to store data, which is an encapsulation of sqlite. However, Apple says it is not a relational database.
Corresponding to the data table:
Table Structure: NSEntityDescription
Contact all tables in the database: NSManagedObjectModel
Database storage method: NSPersistentStoreCoordinator
Database Operation: NSManagedObjectContext
Query statement: NSFetchRequest
Table Record: NSManagedObject
A model is usually defined using the CoreData. xcdatamodel file, which can be operated graphically.
NSPersistentStoreCoordinator class objects are usually initialized using NSManagedObjectModel objects. This class abstracts different storage methods and most often uses NSSQLiteStoreType.
The NSManagedObjectContext class object is initialized using the NSPersistentStoreCoordinator object. It contains some methods to add and delete NSManagedObject.
The NSFetchRequest class generally uses NSEntityDescription to construct a query. You can also specify the sorting.
The NSFetchedResultsController class is used together with NSFetchRequest to facilitate data retrieval. In addition, it is associated with NSManagedObjectContext and receives a notification when the database changes.
For more detailed usage and examples, refer to books.