当前位置: 首页 > news >正文

Core Data 的简单使用

认识cocoa Data在ios开发中的环境情况。



Core Data简单使用的样例。可以使用模板中的master—detail 这种控制器组合可以轻松完毕。

主要的文件夹框架:


对应的Core Data中的基本对象都会自己主动生成。


masterController.md的代码:


//
//  MasterViewController.m
//  Tasks
//
//  Created by 朱敏 on 15/8/24.
//  Copyright (c) 2015年 helinyu. All rights reserved.
//

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "TaskEntryViewController.h"

@interface MasterViewController ()

@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender {
    
    TaskEntryViewController *tevc = [[TaskEntryViewController alloc]init];
    tevc.managedObjectContext = self.managedObjectContext;
    
    [self presentViewController:tevc animated:YES completion:nil];
//    NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
//    NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
//    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
//        
//    // If appropriate, configure the new managed object.
//    // Normally you should use accessor methods, but using KVC here avoids the need to add a custom class to the template.
//    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
//        
//    // Save the context.
//    NSError *error = nil;
//    if (![context save:&error]) {
//        // Replace this implementation with code to handle the error appropriately.
//        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
//        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//        abort();
//    }
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        [[segue destinationViewController] setDetailItem:object];
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [[self.fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    [self configureCell:cell atIndexPath:indexPath];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
            
        NSError *error = nil;
        if (![context save:&error]) {
            // Replace this implementation with code to handle the error appropriately.
            // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];

    cell.textLabel.text = [[object valueForKey:@"taskText"] description];
    cell.detailTextLabel.text = [[object valueForKey:@"timeStamp"] description];
}

#pragma mark - Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
//    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:self.managedObjectContext];
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:self.managedObjectContext];
    
    [fetchRequest setEntity:entity];
    
    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];
    
    // Edit the sort key as appropriate.
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = @[sortDescriptor];
    
    [fetchRequest setSortDescriptors:sortDescriptors];
    
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    
	NSError *error = nil;
	if (![self.fetchedResultsController performFetch:&error]) {
	     // Replace this implementation with code to handle the error appropriately.
	     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
	    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
	    abort();
	}
    
    return _fetchedResultsController;
}    

#pragma mark NSFetchedResultsControllerDelegate

- (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;
            
        default:
            return;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;
    
    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
            
        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
            
        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;
            
        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

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

/*
// Implementing the above methods to update the table view in response to individual changes may have performance implications if a large number of changes are made simultaneously. If this proves to be an issue, you can instead just implement controllerDidChangeContent: which notifies the delegate that all section and object changes have been processed. 
 
 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    // In the simplest, most efficient, case, reload the table view.
    [self.tableView reloadData];
}
 */

@end

detail中的代码没有什么改动,创建之后就能够生成,这个我们就无论了


TaskEntryViewController.m文件的代码:

//
//  TaskEntryViewController.m
//  Tasks
//
//  Created by 朱敏 on 15/8/24.
//  Copyright (c) 2015年 helinyu. All rights reserved.
//

#import "TaskEntryViewController.h"

@interface TaskEntryViewController ()

@end

@implementation TaskEntryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//    NSSet 
    self.tf = [[UITextField alloc]initWithFrame:CGRectMake(65, 20, 200, 20)];
    [self.tf setBackgroundColor:[UIColor lightGrayColor]];
    [self.tf setDelegate:self ];
    [self.view addSubview:self.tf];
    
    UILabel *lb1 = [[UILabel alloc]initWithFrame:CGRectMake(5, 20, 60, 20)];
    [lb1 setText:@"Task"];
    
    [self.view addSubview:lb1];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSManagedObjectContext *context = self.managedObjectContext;
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Task" inManagedObjectContext:context];
    NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
    
    [newManagedObject setValue:[NSDate date] forKey:@"timeStamp"];
    [newManagedObject setValue:[self.tf text] forKey:@"taskText"];
    
    NSError *error = nil;
    
    if (![context save:&error]) {
        NSLog(@"Unresolved error %@,%@",error,[error userInfo]);
        abort();
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    return YES;
}

@end

对应的storyBoard的改变的界面:




上面执行就能够成功了。







相关文章:

  • 配置防盗链,访问控制
  • 过完年想要元气满满?赶紧看看这些VR AR大事件回个血
  • RocketMq部署(四)
  • oracle时间操作结合to_char和to_date使用
  • VS2017 Debug断点后显示UTF8字符串
  • 拥抱.NET Core系列:MemoryCache 缓存选项
  • 树莓派打造无线扫描仪.
  • 用“世界上最好的编程语言”制作的敲诈者木马揭秘
  • Docker容器部署时区问题的坑
  • 【转】Tesla Model S的设计失误
  • JavaScript 奇技淫巧
  • 测试经验1_2016-2017
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • EF Core:一统SQL和NoSQL数据库
  • linux----------fedora 27 如何启用输入法
  • Android 控件背景颜色处理
  • AzureCon上微软宣布了哪些容器相关的重磅消息
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • HTTP中的ETag在移动客户端的应用
  • IP路由与转发
  • k8s如何管理Pod
  • Node 版本管理
  • Object.assign方法不能实现深复制
  • rabbitmq延迟消息示例
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Spring核心 Bean的高级装配
  • supervisor 永不挂掉的进程 安装以及使用
  • 基于MaxCompute打造轻盈的人人车移动端数据平台
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 新书推荐|Windows黑客编程技术详解
  • 字符串匹配基础上
  • Hibernate主键生成策略及选择
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ​比特币大跌的 2 个原因
  • #《AI中文版》V3 第 1 章 概述
  • #pragma once与条件编译
  • #宝哥教你#查看jquery绑定的事件函数
  • $redis-setphp_redis Set命令,php操作Redis Set函数介绍
  • (2)nginx 安装、启停
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (六)Hibernate的二级缓存
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (三十五)大数据实战——Superset可视化平台搭建
  • (四) 虚拟摄像头vivi体验
  • (一)kafka实战——kafka源码编译启动
  • (最简单,详细,直接上手)uniapp/vue中英文多语言切换
  • **登录+JWT+异常处理+拦截器+ThreadLocal-开发思想与代码实现**
  • .net core 源码_ASP.NET Core之Identity源码学习
  • .net core开源商城系统源码,支持可视化布局小程序
  • .net 写了一个支持重试、熔断和超时策略的 HttpClient 实例池
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)