[ios]Core Data

先看概念详解:http://blog.csdn.net/remote_roamer/article/details/7015826

//表格结构    --> NSEntityDescription
//数据库中所有表格和他们的联系 -->NSManagedObjectModel
//数据库存放方式 --> NSPersistentStoreCoordinator
//数据库操作 --> NSManagedObjectContext
//查询语句 --> NSFetchRequest
//表格的记录 --> NSManagedObject

有一个学生类叫Student里面有name ,age,hoby字段

下面用core data对3个字段操作。

-(void) setup{
    //初始化model
    NSURL *modelURL=[[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
    self.managedObjectModel=[[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL ] autorelease];
    self.persistentStoreCoordinator=[[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel ] autorelease]; //调节器和model关联起来
    NSURL *storeUrl=[NSURL  fileURLWithPath:[[self appDocumentDirectory] stringByAppendingPathComponent:@"Model.sqlite"]];
    NSError * error=nil;
    [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error];
    
    self.managedObjectContext=[[[NSManagedObjectContext alloc] init ] autorelease ];
    [self.managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
    
    NSFetchRequest *fetchRequest=[[[NSFetchRequest alloc] init] autorelease];
    NSEntityDescription * entity=[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:self.managedObjectContext] ;

    //这里entiy名字叫 Entity 自己要添加(这里省略)
    [fetchRequest setEntity:entity];
    NSSortDescriptor * sort=[[[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES ]autorelease ];
    //对象的排序功能
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    self.fetchedResultsController=[[[NSFetchedResultsController alloc]
    initWithFetchRequest:fetchRequest
    managedObjectContext:self.managedObjectContext
    sectionNameKeyPath:nil
                           cacheName:nil] autorelease];
}

-(void)addEmplyee:(NSString *) name age: (NSInteger) age hobby:(NSString *) hooby
{
 
    Student * stu=[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];
    stu.name=name;
    stu.age=[NSNumber numberWithInteger:age];

    stu.hooby=hooby;
    NSError*error=nil;
    if(![self.managedObjectContext save:&error])
    {
    
        NSLog(@"Save ErrorL %@",[error localizedDescription]);
    }
    


}

-(void)removeEmplyee:(NSString *)name
{

    NSFetchRequest *request=[[[NSFetchRequest alloc] init ]autorelease ];
    NSEntityDescription * entity=[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:self.managedObjectContext];
    [request setEntity:entity];
    NSPredicate * predicate=[NSPredicate predicateWithFormat:@"name==[c]%@",name];
    [request setPredicate:predicate];
    NSError *error=nil;
    NSArray * array=[self.managedObjectContext executeFetchRequest:request error:&error];
    if(array.count>0)
    {
        Student * stu=[array objectAtIndex:0];
        [self.managedObjectContext deleteObject:stu];
        if(![self.managedObjectContext save:&error ])
        {
        
        NSLog(@"Save: %@", [error localizedDescription]);
        
        }
    
    }


}

-(NSArray *)fetchAllEmplyee
{


    NSError* error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {
        NSLog(@"performFetch:%@", [error localizedDescription]);
    }
    return [fetchedResultsController fetchedObjects];
}

原文地址:https://www.cnblogs.com/jinjiantong/p/2981422.html