您的位置:首页 > Web前端

NSFetchedResultController讲解

2015-08-24 19:52 225 查看
有关于coreData 中可以说为在外界的代码和Core Data进行打交道最为主要的类就是NSFetchedResultController,可以说是“中枢”,

起到了承上启下的作用。

它的一些相应的方法常用到的写如下,可供参考:

#import <Foundation/Foundation.h>

//接口
@protocol NSFetchedResultsControllerDelegate;

//所需要的底层的两个对象
@class NSFetchRequest;  //发送请求
@class NSManagedObjectContext; //上下文(通过请求上下文)

//UItableView中相关联

NSFetchedResultsController
//讲解:它工作在高层中的视图的原理:将一个“请求”和一个“上下文”作为其输入;
//并在请求中的数据改变的时候调用相应的代理方法。

NSFetchRequest + NSManagedObjectContext —— NSFetchedResultsController ——> (获取的结果控制器委托(NSFetchedResultsControllerDelegate方法))(获取结果)——————>UItableView

//这个获取结的果控制器对象私有属性
@interface NSFetchedResultsController : NSObject {
@private
	NSFetchRequest *_fetchRequest;
	NSManagedObjectContext *_managedObjectContext;
	NSString *_sectionNameKeyPath;
	NSString *_sectionNameKey;
	NSString *_cacheName;
	void	  *_cache;
	struct _fetchResultsControllerFlags {
	  unsigned int _sendObjectChangeNotifications:1;
	  unsigned int _sendSectionChangeNotifications:1;
	  unsigned int _sendDidChangeContentNotifications:1;
	  unsigned int _sendWillChangeContentNotifications:1;
	  unsigned int _sendSectionIndexTitleForSectionName:1;
	  unsigned int _changedResultsSinceLastSave:1;
	  unsigned int _hasMutableFetchedResults:1;
	  unsigned int _hasBatchedArrayResults:1;
	  unsigned int _hasSections:1;
	  unsigned int _usesNonpersistedProperties:1;
	  unsigned int _includesSubentities:1;
	  unsigned int _reservedFlags:21;
	} _flags;
	id _delegate;
	id _sortKeys;
	id _fetchedObjects;
	id _sections;
	id _sectionsByName;
	id _sectionIndexTitles;
	id _sectionIndexTitlesSections;
	
}

//获取结果改变的类型
typedef NS_ENUM(NSUInteger, NSFetchedResultsChangeType) {
	NSFetchedResultsChangeInsert = 1,  //插入
	NSFetchedResultsChangeDelete = 2,  //删除
	NSFetchedResultsChangeMove = 3,    //移动
	NSFetchedResultsChangeUpdate = 4   //更新
} NS_ENUM_***AILABLE(NA,3_0);

- (id)initWithFetchRequest:(NSFetchRequest *)fetchRequest managedObjectContext: (NSManagedObjectContext *)context sectionNameKeyPath:(NSString *)sectionNameKeyPath cacheName:(NSString *)name;

##################下面是相应的代理方法
#pragra mark  NSFetchedResultsControllerDelegate 
@protocol NSFetchedResultsControllerDelegate
@optional
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller;
//就是这个更新,将会通过tableView中的的-beginUpdates方法来更行表图的显示。

@optional
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller;
//已经改变了之后

//这个事相对于 “行” 来操作的
@optional
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath;

 //这个方法是相对于 “段” 为单位来操作的
@optional
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type;
//

@optional
- (NSString *)controller:(NSFetchedResultsController *)controller sectionIndexTitleForSectionName:(NSString *)sectionName ;
//设置段的美国名字

//这个就是发送请求的方法
- (BOOL)performFetch:(NSError **)error;

//这个属性,可见一个实例中的属性实体的排序方式,可以是几个不同的属性字段综合排序
@property (nonatomic, strong) NSArray *sortDescriptors;

timeStamp
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: