码迷,mamicode.com
首页 > 其他好文 > 详细

CoreData的基本使用

时间:2015-08-12 01:12:09      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

  

  CoreData:是苹果的原生框架, 就理解为管理数据模型用的吧。可以替代SQLite,使用它就再也不需要写什么SQLite语句了。面向对象,更好用!

不管那么多直接来使用吧~~

第一步,在创建项目的时候,可以勾选coreData。系统会自动生成coreData文件。

技术分享

第二步,在项目中找到格式为xcdatamodeld的文件 ,添加entity实体,这个实体就是模型,最后可以生成平时使用的模型文件。 

技术分享

设置类名

技术分享

添加属性

技术分享

生成文件

技术分享

 

第三步:可以去appDelegate中看看一些不一样的东西。苹果自动生成了一些东西,不用更改直接使用即可。

增加数据代码:

#import "ViewController.h"
#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Person.h"
#import "Company.h"
@interface ViewController ()

@property(nonatomic, weak) AppDelegate *app;//包含代理对象

@end

@implementation ViewController

//懒加载代理对象
- (AppDelegate *)app{
    return [UIApplication sharedApplication].delegate;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //从管理对象集合中获取模型对象
   Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.app.managedObjectContext];
    //设置属性
    person.name = @"wangning";
    person.age = @(55);
    person.phone = @"110";
    
    //保存数据,此时数据就保存在了SQLite中了。
    [self.app saveContext];
    NSLog(@"保存路径--%@",[self.app applicationDocumentsDirectory]);

}

 查询数据代码:查询需要使用到一个特别的类--

NSFetchedResultsController

#import <CoreData/CoreData.h>
#import "AppDelegate.h"
#import "Person.h"
#import "Company.h"
@interface WNTableViewController ()<NSFetchedResultsControllerDelegate,UISearchBarDelegate>
@property(nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
@property (weak, nonatomic) IBOutlet UISearchBar *seachBar;

@property(nonatomic, strong) AppDelegate *appDelegate;

@end

@implementation WNTableViewController

- (AppDelegate *)appDelegate{

    return [UIApplication sharedApplication].delegate;
}
//懒加载查询结果控制器
- (NSFetchedResultsController *)fetchedResultsController{

    if (!_fetchedResultsController) {
        
        //查询请求,查询“Person”类
        NSFetchRequest *requst = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
        
        //设置查询排序,一定要设置!!
        NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"personName" ascending:YES];
        //将排序添加到查询请求
        requst.sortDescriptors = @[sort];
        
        //实例化
        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:requst managedObjectContext:self.appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
        
        //设置代理,一般都是监听数据改变。还可以做其他事情
        _fetchedResultsController.delegate = self;
    }
    return _fetchedResultsController;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //准备查询,在需要的位置设置,这也是必须的步骤。
    [self.fetchedResultsController performFetch:NULL];
}

#pragma mark 查询代理方法
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller{

    //当数据发生改变时,刷新数据
    [self.tableView reloadData];
}

将查询到的数据在cell中显示:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //查询到的数据个数fetchedObjects
    return self.fetchedResultsController.fetchedObjects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    Person *person = self.fetchedResultsController.fetchedObjects[indexPath.row];
    
    cell.textLabel.text = person.personName;
    cell.detailTextLabel.text = person.company.companyName;
    
    return cell;
}

  如果要使用coreData进行模糊查询,需要使用到谓词。这个下次再写。。。

CoreData的基本使用

标签:

原文地址:http://www.cnblogs.com/jingdian/p/4722857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!