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 相关知识

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

iOS项目发布提交出现invalid code signing entitlements错误。

1、进入开发者账号,选择App IDs,找到自己项目对应的AppId,点击进去编辑, 2、看下错误提示出现  --Specifically, value "CVYZ6723728.*" for key "com.apple.developer.ubiquity-container-identifiers" in XX is not supported.-- 这样的错误提示 将ubiquity

我的第一次份实习工作-iOS实习生-第三个月

第三个月 这个月有一个考核项目,是一个电子书阅读器,组长说很重要,是我的实习考核项目。 我的项目XTReader,这是我参考网上的一些代码,和模仿咪咕阅读做的,功能还不完善,数据的部分是用聚合数据做的。要收费的。   还有阅读页面,基本功能实现了一下。使用了autolayout,自适应布局,也是第一次用网络,第一次用数据库,第一次用自动布局。还有很多不足。 做了一周多,有个问题一直没

我的第一次份实习工作-iOS实习生-公司使用过的软件

bittorrentsync 素材,文件同步软件 cornerstone svn 软件开发合作 mark man 测量坐标的软件 SQLLite Manager 数据库操作软件

我的第一次份实习工作-iOS实习生-第二个月

第二个月 来公司过了一个月了。每天早上9点上班,到晚上6.30下班,上下班要指纹打卡,第一个月忘了打卡好多次(),然后还要去补打卡单。公司这边还安排了,工资卡办理,招商银行卡。开了一次新员工大会,认识了公司的一些过往,公司的要求等,还加了一下公司的企业QQ,还有其他的羽毛球群,篮球群。我加了下羽毛球群,也去打了一两次。第二个月的感受,感觉跟组里面的交流跟沟通都好少,基本上还有好多人不认识。想想也

我的第一次份实习工作-iOS实习生-第一个月

实习时间:2015-08-20 到 2015-12-25  实习公司;福建天棣互联有限公司 实习岗位:iOS开发实习生 第一个月: 第一天来公司,前台报道后,人资带我去我工作的地方。到了那,就由一个组长带我,当时还没有我的办公桌,组长在第三排给我找了一个位置,擦了下桌子,把旁边的准备的电脑帮我装了下,因为学的是iOS,实习生就只能用黑苹果了,这是我实习用的电脑。 帮我装了一下电脑后,开机