ios coredata sqlite3 NSFetchedResultsController(2)

2024-02-17 17:58

本文主要是介绍ios coredata sqlite3 NSFetchedResultsController(2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//
//  RootViewController.m
//  FetchedResultsController
//
//  Created by 何瑾 on 15/1/16.
//  Copyright (c) 2015年 e世雕龙. All rights reserved.
//#import "RootViewController.h"
#import "Student.h"@interface RootViewController ()@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;// 托管对象上下文
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;// 抓取结果控制器@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];// 1、获取应用程序对象(单例)UIApplication *app = [UIApplication sharedApplication];// 2、获取应用程序委托id appDelegate = app.delegate;// 3、获取托管对象上下文self.managedObjectContext = [appDelegate managedObjectContext];// 4、创建抓取请求对象NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Student"];// 4.1、创建排序规则NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];// 4.2、设置抓取请求对象的排序规则属性[request setSortDescriptors:@[sd1]];// 5、创建抓取结果控制器(1、给表视图提供数据。2、监控CoreData数据变化,回调四个委托方法)self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"stu"];// 5.1指定抓取结果控制器的委托,在CoreData中对象发生变化时调用相关委托方法self.fetchedResultsController.delegate = self;// 6、执行抓取__autoreleasing NSError *error = nil;[self.fetchedResultsController performFetch:&error];if (error) {[self myAlert:[error localizedDescription]];}
}#pragma mark 通过CoreData添加对象
- (IBAction)addObject:(id)sender {//使用实体描述对象把一个空的数据模型(托管对象)放入托管对象上下文中Student *stu = [NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:self.managedObjectContext];//设置托管对象属性值static int i = 0;i++;stu.name = [NSString stringWithFormat:@"何瑾%d",i];stu.phoneNumber = @18888888888;//保存托管对象上下文__autoreleasing NSError *error = nil;[self.managedObjectContext save:&error];if (error) {NSLog(@"%@",[error localizedDescription]);[self myAlert:[error localizedDescription]];}
}#pragma mark - Table view data source- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return [[self.fetchedResultsController sections] count];
}- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {if ([[self.fetchedResultsController sections] count] > 0) {id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];return [sectionInfo numberOfObjects];} elsereturn 0;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {// 创建出列可重用标识符static NSString *cellID = @"cellID";// 从出列可重用队列中获取单元格UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];// 如果没有则创建if (!cell) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];}Student *stu = [self.fetchedResultsController objectAtIndexPath:indexPath];// Configure the cell with data from the managed object.cell.textLabel.text = stu.name;cell.detailTextLabel.text = [stu.phoneNumber stringValue];return cell;
}/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the specified item to be editable.return YES;
}
*/// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) {// Delete the row from the data source// 只需要删除CoreData中对象// 从CoreData的托管对象上下文中获取模型对象Student *stu = [self.fetchedResultsController objectAtIndexPath:indexPath];// 从CoreData的托管对象上下文中删除对象[self.managedObjectContext deleteObject:stu];// 保存上下文__autoreleasing NSError *error = nil;[self.managedObjectContext save:&error];if (error) {[self myAlert:[error localizedDescription]];}} else if (editingStyle == UITableViewCellEditingStyleInsert) {// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view}   
}/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*//*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {// Return NO if you do not want the item to be re-orderable.return YES;
}
*//*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*/
#pragma Table view delegate
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {return @"删除";
}
#pragma mark 封装UIAlertView
- (void)myAlert:(NSString *)errorMsg {UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"信息" message:errorMsg delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];[alert show];
}
#pragma mark NSFetchedResultsControllerDelegate methods
/*Assume self has a property 'tableView' -- as is the case for an instance of a UITableViewControllersubclass -- and a method configureCell:atIndexPath: which updates the contents of a given cellwith information from a managed object at the given index path in the fetched results controller.*/
// 告知tableView开始进行数据变化,此时tableView将会记录变化点,当所有变化结束之后,统一进行UI的刷新
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {[self.tableView beginUpdates];
}// 表视图分区变化回调此委托方法
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfoatIndex:(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;}
}// 根据CoreData数据变化类型进行表视图更新(增、删、改、移动)
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObjectatIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)typenewIndexPath:(NSIndexPath *)newIndexPath {UITableView *tableView = self.tableView;switch(type) {// 增加case NSFetchedResultsChangeInsert:[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];break;// 删除case NSFetchedResultsChangeDelete:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];break;// 修改case NSFetchedResultsChangeUpdate:[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];break;// 移动case NSFetchedResultsChangeMove:[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationFade];[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationFade];break;}
}// 通知tableView数据变化结束,开始进行UI刷新
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {[self.tableView endUpdates];
}
@end

这篇关于ios coredata sqlite3 NSFetchedResultsController(2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/718565

相关文章

SQLite3 在嵌入式C环境中存储音频/视频文件的最优方案

《SQLite3在嵌入式C环境中存储音频/视频文件的最优方案》本文探讨了SQLite3在嵌入式C环境中存储音视频文件的优化方案,推荐采用文件路径存储结合元数据管理,兼顾效率与资源限制,小文件可使用B... 目录SQLite3 在嵌入式C环境中存储音频/视频文件的专业方案一、存储策略选择1. 直接存储 vs

sqlite3 命令行工具使用指南

《sqlite3命令行工具使用指南》本文系统介绍sqlite3CLI的启动、数据库操作、元数据查询、数据导入导出及输出格式化命令,涵盖文件管理、备份恢复、性能统计等实用功能,并说明命令分类、SQL语... 目录一、启动与退出二、数据库与文件操作三、元数据查询四、数据操作与导入导出五、查询输出格式化六、实用功

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Pandas使用SQLite3实战

《Pandas使用SQLite3实战》本文主要介绍了Pandas使用SQLite3实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学... 目录1 环境准备2 从 SQLite3VlfrWQzgt 读取数据到 DataFrame基础用法:读

sqlite3 相关知识

WAL 模式 VS 回滚模式 特性WAL 模式回滚模式(Rollback Journal)定义使用写前日志来记录变更。使用回滚日志来记录事务的所有修改。特点更高的并发性和性能;支持多读者和单写者。支持安全的事务回滚,但并发性较低。性能写入性能更好,尤其是读多写少的场景。写操作会造成较大的性能开销,尤其是在事务开始时。写入流程数据首先写入 WAL 文件,然后才从 WAL 刷新到主数据库。数据在开始

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

ubunt下安装sqlite3

// 在线安装 1.安装sqlite3  sudo apt-get install sqlite3 2.安装libsqlite3-dev sudo apt-get install libsqlite3-dev 否则在编译的时候会出现错误:error:sqlite3.h No such file or directory

iOS剪贴板同步到Windows剪贴板(无需安装软件的方案)

摘要 剪贴板同步能够提高很多的效率,免去复制、发送、复制、粘贴的步骤,只需要在手机上复制,就可以直接在电脑上 ctrl+v 粘贴,这方面在 Apple 设备中是做的非常好的,Apple 设备之间的剪贴板同步功能(Universal Clipboard)确实非常方便,它可以在 iPhone、iPad 和 Mac 之间无缝传输剪贴板内容,从而大大提高工作效率。 但是,iPhone 如何和 Wind