您的位置:首页 > 移动开发 > IOS开发

IOS学习高级课程(二)coredata

2014-05-06 20:36 375 查看
使用coredata步骤:
1.配置数据模型数据模型:
增加实体:(删除实体--style里删除)
命名实体(首字母必须大写):

2.创建数据模型对象
    选择模型对象

   选择实体  (实体名:Student) 

3.用数据模型对象创建持续储存协调器

 4.用持续储存协调器创建储存文件

        5.创建托管对象上下文并配置储存储存协调器
          在点.h中:
     @property (strong,
nonatomic)
NSManagedObjectContext *managedObjectContext;
         在点.m中: 
         //获取托管对象上下文
    UIApplication * app = [UIApplication
sharedApplication];
    id delegate = app.delegate;
    self.managedObjectContext = [delegate
managedObjectContext];

        6.使用上下文进行增删改查操作
    增:   
     //添加一个空对象到上下文中
   Student * stu =  [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.managedObjectContext];
   //改
   stu.name = @“ly”;
   …………………………………….
   //保存上下文,写入到储存文件中
   __autoreleasing NSError * error;//如果是arc,创建对象后,默认为nil
   [self.managedObjectContext save:&error];
   查:
    //查:年龄为20的学生
    NSFetchRequest * request = [[NSFetchRequest
alloc]initWithEntityName:@"Student"];
    //设置谓词
    int i =
20;
    NSPredicate * predicate = [NSPredicate
predicateWithFormat:@"age=%d",i];
    //设置排序规则(当有多个年龄为20时,按name排序)
    //创建排序描述
    NSSortDescriptor * sortD1 = [[NSSortDescriptor alloc]initWithKey:@"name" ascending:YES];
    [request setPredicate:predicate];
    __autoreleasing
NSError * error;
    //返回查找的值
    NSArray *result= [self.managedObjectContext
executeFetchRequest:request
error:&error];
    删:(先查找出来托管对象上下文再删除)
    //获取查找后数组的最后一个元素
    Student * stu = [result lastObject];
    if (stu !=
nil)
    {
        //删除--哪个托管对象
        [self.managedObjectContext
deleteObject:stu];
        error = nil;
        [self.managedObjectContext
save:&error];
        
    }
    else
    {
        NSLog(@"无数据");
    }

 7.保存上下文状态   __autoreleasing NSError *
error;//如果是arc,创建对象后,默认为nil 
 [self.managedObjectContext save:&error];

 8.coredata&表视图结合使用
   NSFetchedResultsController,监控coredata的数据变换,给tableView提供数据
   NSFetchRequest * request = [[NSFetchRequest
alloc]initWithEntityName:@"Student"];
   NSSortDescriptor * sort = [[NSSortDescriptor
alloc]initWithKey:@"name"
ascending:YES];
    [request setSortDescriptors:@[sort]];
     self.fetchedResultsController = [[NSFetchedResultsController
alloc]initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:@"stu"];//cacheName表示有缓存
          //设置代理
   self.fetchedResultsController.delegate
= self;
   __autoreleasing
NSError * error;
   //让这个Controller开始工作:监听和提供数据
   [self.fetchedResultsController
performFetch:&error]
- (void)controllerWillChangeContent:(NSFetchedResultsController
*)controller
{
    [self.tableView
beginUpdates];
}

//分区改变
- (void)controller:(NSFetchedResultsController
*)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type 
{
   switch(type)
   {
     case
NSFetchedResultsChangeInsert:
     [self.tableView
insertSections:[NSIndexSet
indexSetWithIndex:sectionIndex]
                        withRowAnimation:UITableViewRowAnimationFade];
            break;
     case
NSFetchedResultsChangeDelete:
     [self.tableView
deleteSections:[NSIndexSet
indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

 //某一个对象的数据发生改变
- (void)controller:(NSFetchedResultsController
*)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath 
  {
    UITableView *tableView =
self.tableView;
    switch(type)
    {
        //如果插入一个数据,那么tableView就要插入一行
        case
NSFetchedResultsChangeInsert:
        [tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath]                           withRowAnimation:UITableViewRowAnimationFade];
         break;
        //删除一个数据,删除一行
        case
NSFetchedResultsChangeDelete:
             [tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath]                           withRowAnimation:UITableViewRowAnimationFade];
         break;
        //数据更新---newIndexPath!!!
        case
NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
            break;
            
        case
NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath]
                             withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController
*)controller
{
    [self.tableView
endUpdates];
}

           名词解释:
               托管对象上下文:你可以把它当作是一个数据库取出对象的“暂存器”。基本上,无论何时需要获取对象,插入 对象或是删除对象,你都要调用管理对象上下文(managed object context)的方法
               .持续储存协调器:用数据模型对象来创建持续储存文件你可以把它当作是数据库的连接。在这里你设定用来存储对象的数据库的真实名称和位置,以及需要时用来保存的管理对象上下文(managed object context) 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: