与 integated 核心数据视图集成的便捷持续性,以及支持可选的 RESTful 后端。TOMSCoreDataManager 为您的自定义 TableViewController 或 CollectionViewController 实现以及有用的 ManagedObject 扩展提供有用的超类。
platform :ios, '7.0'
pod "TOMSCoreDataManager", "~> 0.1.4"
您可以将项目导入到项目的预编译头文件中,使 TOMSCoreDataManager 的所有组件在整个项目中都可用。
#import <TOMSCoreDataManager/TOMSCoreDataManager.h>
TOMSCoreDataTableViewController
和 TOMSCoreDataCollectionViewController
为子类提供了一个非常好的起点。只需从适当的超类继承,并实现以下数据源方法:(以下示例假设有一个 Model.xcdatamodel,它定义了一个具有两个属性 objectId
和 name
的实体 Person
)
/**
The modelName specifies the name of the model, that contains the displayed entities.
*/
- (NSString *)modelName
{
return @"Model";//without .xcdatamodel extension
}
/**
The entityName specifies the entities that should be displayed by the table (or collection) view.
Usually those entityNames are the class names of the generated NSManagedObject subclasses.
*/
- (NSString *)entityName
{
return @"Person";
}
/**
The cellIdentifierForItemAtIndexPath specifies the cell identifier for one specific indexPath.
This can be used to work with your custom styled cells from the interface builder.
*/
- (NSString *)cellIdentifierForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * const cellIdentifier = @"Cell";
return cellIdentifier;
}
/**
Provide a defaultPredicate to specify which data should be fetched if there is no custom or predicate present at a time.
*/
- (NSPredicate *)defaultPredicate
{
return [NSPredicate predicateWithFormat:@"name.length > 0"];
}
/**
Provide defaultSortDescriptors to specify in which order data should be presented by default.
*/
- (NSArray *)defaultSortDescriptors
{
return @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]];
}
/**
configureCell:forIndexPath: is getting called by the superclass while setting a cell up.
Use this method as entry point to configure the contents of a cell at a specific indexPath.
*/
- (void)configureCell:(id)cell
forIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = (UITableViewCell *)cell;
NSManagedObject *object = [self.coreDataFetchController objectAtIndexPath:indexPath];
if ([object isKindOfClass:[Person class]]) {
Person *person = (Person *)object;
tableViewCell.textLabel.text = person.name;
tableViewCell.detailTextLabel.text = person.objectId;
}
}
coreDataFetchController
是前面两个 ViewControllers 的私有属性。可用于轻松影响和更新可见数据集。为此,您可以同时设置 predicate
、sortDescriptors
或两者均设置。这样做很简单,因为它们是 coreDataFetchController
的属性,并且可以通过点语法访问。
设置 predicate 将在内部执行对数据库的查询请求。当前表或 collectionView 将自动并自主地更新其内容。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@", @"Steve"]
self.coreDataResultsController.predicate = predicate;
设置 sortDescriptors 将适当地重新排序表或 collectionViews 显示的数据。
NSArray *sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]
self.coreDataResultsController.sortDescriptors = sortDescriptors;
由于设置 predicate
或 sortDescriptors
会立即生效,因此以下辅助方法用于同时设置两者。
[self.coreDataResultsController setPredicate:predicate sortDescriptors:sortDescriptors];
这个分类提供了一些有用的方法。特别有用的是将 NSManagedObject+TOMSHelpers.h
添加到你的自定义 NSManagedObject
子类中。此分类使你能够在自定义模型上调用以下辅助方法。
/**
Finds and returns an object for the given unique identifier in a context.
*/
+ (instancetype)toms_objectForUniqueIdentifier:(NSString *)uniqueIdentifier
inContext:(NSManagedObjectContext *)context;
/**
Creates an object for the given associative dictionary.
The dictionaries keys should match the entities property names.
The context is saved automatically after inserting the object.
*/
+ (instancetype)toms_newObjectFromDictionary:(NSDictionary *)dictionary
inContext:(NSManagedObjectContext *)context;
/**
Creates an object for the given associative dictionary.
The dictionaries keys should match the entities property names.
The context is saved automatically after inserting the object if specified.
*/
+ (instancetype)toms_newObjectFromDictionary:(NSDictionary *)dictionary
inContext:(NSManagedObjectContext *)context
autoSaveContext:(BOOL)autoSave;
/**
Returns an array of instances that match the predicate, sorted by the sort descriptors.
*/
+ (NSArray *)toms_objectsForPredicate:(NSPredicate *)predicate
sortDescriptors:(NSArray *)sortDescriptors
inContext:(NSManagedObjectContext *)context;
/**
Returns an array of instances that match the predicate.
*/
+ (NSArray *)toms_objectsForPredicate:(NSPredicate *)predicate
inContext:(NSManagedObjectContext *)context;
请注意,在一个 TOMSCoreData{Table, Collection}ViewController
中,可以通过其 managedObjectContext
属性轻松访问相应的 context
。通过 TOMSHelpers
分类创建的所有对象都将自动生成唯一标识符。
Person *steve = [Person toms_newObjectFromDictionary:@{
@"name" : @"Steve"
}
inContext:self.managedObjectContext];
TOMSCoreDataManager 假定所有实体都有一个名为 objectId
的属性来表示唯一标识符。但是,如果你想指定唯一标识符的另一个属性名称,或者你不想让对象具有唯一可识别性,你可以在 NSManagedObject 子类中实现以下方法之一。
#pragma mark - optional implementation
/**
The returned string has to match an attribute of the entity. This will be assumed to be the objects unique identifier.
*/
+ (NSString *)toms_uniqueIdentifier
{
return @"uniqueIdentifier";
}
/**
If you return NO, there will be no unique identifiers generated for this particular entity.
*/
+ (BOOL)toms_shouldAutoGenerateGloballyUniqueIdentifiers
{
return NO;
}
为了同步本地数据库与 RESTful web 服务,你可以实现可选的 DataSource 方法指定你的子类化的 AFRestClient。要做到这一点,你只需将 backingRESTClientClass
添加到你的 TOMSCoreData{Table, Collection}ViewController.m
,并返回你实现类的类。
- (Class)backingRESTClientClass
{
return [YourRestClient class];
}
由于 CoreData 堆栈初始化自己的 YourRestClient
实例,因此重写简单的 init
方法并在其中通过调用父类的 initWithBaseURL
初始化自己非常重要。初始化器可能如下所示
- (id)init
{
self = [super initWithBaseURL:[NSURL URLWithString:@"https://yourapp.herokuapp.com"]];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
}
return self;
}
按照 AFIncrementalStore 项目页面上的指南,可以轻松完成其余客户端的实施。
TOMSCoreDataManager 依赖于 Mattt Thompson 的 AFIncrementalStore。感谢这一点!
TOMSCoreDataManager 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。