My personal feeling about coreData technology is that it is troublesome, for beginners? Always use:
The three core classes of coreData:
1. Initialize the NSManagedObjectModel object, load the model file, and read all entity information in the app 2. Initialize the NSPersistentStoreCoordinator object and add a persistence library (SQLite database is used here) 3. Initialize the NSManagedObjectContext object, get the context object to operate the entity, and perform CRUD operations
1. Core creation and insertion;
self.appDelegate=[UIApplication sharedApplication ].delegate;
-(void)insertData{ NSLog(@" start insertData()"); //1, create a model //create entity description object NSEntityDescription *entityDesription=[NSEntityDescription entityForName:@"Product" inManagedObjectContext:self.appDelegate.managedObjectContext]; //Parameter 1: NSEntityDescription specifies the model //Parameter two: (nullable NSManagedObjectContext *) context Product *product=[[Product alloc] initWithEntity:entityDesription insertIntoManagedObjectContext:self.appDelegate.managedObjectContext]; //2, attribute assignment product.pname=@"Gold"; // int radnom=arc4random()%1000+1;//Random numbers from 1 to 1000 // product.ccbprice=[NSNumber numberWithInt:radnom]; // [self.arrayData addObject:product]; // save the data permanently [self.appDelegate saveContext]; NSLog(@" end insertData()"); // [self selectData];//Query data }
operation result:
2016-03-23 22:45:53.813 CoreData2[573:10522] start insertData()
2016-03-23 22:45:53.831 CoreData2[573:10522] end insertData()
2. Query of coreData
Query data: NSFetchRequest
Sort: NSSortDescriptor
#pragma mark //Query data -(void)selectData{ //1, specify the query object //parameter one: query entity NSFetchRequest *selectRequest=[[NSFetchRequest alloc] initWithEntityName:@"Product"]; // //2, specify the sorting representation object // //Parameter 1: Sort field // //Parameter two; whether ascending order yes // NSSortDescriptor *sortDesriptor=[[NSSortDescriptor alloc]initWithKey:@"ccbprice" ascending:YES]; // // //3, set the order of the request // selectRequest.sortDescriptors=@[sortDesriptor]; //4, execute the query NSError *error=nil; NSArray *selectResouce= [self.appDelegate.managedObjectContext executeFetchRequest:selectRequest error:&error]; [self.arrayData addObjectsFromArray:selectResouce]; for (int n=0; n<[self.arrayData count]; n++) { Product *et=[self.arrayData objectAtIndex:n]; NSLog(@"%@--",et.pname); }}
operation result:
2016-03-23 22: 45: 53.832 CoreData2 [573: 10522 ] Golden--
2016-03-23 22: 45: 53.832 CoreData2 [573: 10522 ] Golden--
2016-03-23 22: 45: 53.832 CoreData2 [573: 10522 ] Golden--
2016-03-23 22: 45: 53.832 CoreData2 [573: 10522 ] Golden--
3. The deletion of coreData All operations of coreData use the context,
After deleting the data, you must save the context, similar to the commit of the database
-(void)delectData{ Product *et=[self.arrayData objectAtIndex:8]; [self.appDelegate.managedObjectContext deleteObject:et]; //delete data // save the deleted data [self.appDelegate saveContext]; NSLog(@"deleted"); }
operation result:
2016-03-23 22:56:32.481 CoreData2[659:17475] 黄金++--
2016-03-23 22:56:32.483 CoreData2[659:17475] 删除了
4,更新数据
修改第6条的数据
-(void)updataData{ //更新数据 Product *et=[self.arrayData objectAtIndex:5]; et.pname=@"百合不是茶"; //保存删除的数据 [self.appDelegate saveContext]; NSLog(@"end updataData ()"); }
运行前:
2016-03-23 23:01:12.119 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.119 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金++--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金++--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金++--
2016-03-23 23:01:12.120 CoreData2[692:20232] 黄金++--
2016-03-23 23:01:12.122 CoreData2[692:20232] 删除了
2016-03-23 23:01:12.124 CoreData2[692:20232] end updataData ()
运行后:
2016-03-23 23:02:07.454 CoreData2[696:20826] 黄金--
2016-03-23 23:02:07.454 CoreData2[696:20826] 黄金--
2016-03-23 23:02:07.454 CoreData2[696:20826] 黄金--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金--
2016-03-23 23:02:07.455 CoreData2[696:20826] 百合不是茶--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金++--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金++--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金++--
2016-03-23 23:02:07.455 CoreData2[696:20826] 黄金++--
2016-03-23 23:02:07.457 CoreData2[696:20826] 删除了
2016-03-23 23:02:07.458 CoreData2[696:20826] end updataData ()