DVCoreDataFinders 0.6

DVCoreDataFinders 0.6

测试已测试
Lang语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2014年12月

Peyman Oreizy 维护。



  • 作者:
  • Peyman Oreizy

CoreData API 强大且描述详尽。DVCoreDataFinders 为 NSManagedObject 添加了一些有用的快捷方法来查找对象,创建 NSFetchRequestNSFetchedResultsController 对象,插入对象等。

一些示例:

// find all JournalEntry objects
NSArray *entries = [JournalEntry findAllInContext:managedObjectContext error:nil];

// find all JournalEntry objects matching a predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"author = %@", email];
NSArray *entries = [JournalEntry findAllWithPredicate:predicate inContext:managedObjectContext error:nil];

// find the JournalEntry with id = 8
JournalEntry *entry = [JournalEntry findFirstWhereProperty:@"id" equals:@(8) inContext:managedObjectContext error:nil];

// insert a JournalEntry object
JournalEntry *entity = [JournalEntry insertIntoContext:managedObjectContext];
entity.title = @"a title";
entity.createdAt = [NSDate date];
...

// insert an new object if none found
JournalEntry *entry = [JournalEntry findFirstOrInsertWhereProperty:@"author" equals:@@"[email protected]" insertBlock:^(JournalEntry *createdObject) {
  createdObject.author = @"[email protected]";
  createdObject.body = @"this is an example";
  ...
} inContext:self.managedObjectContext error:nil];

全局过滤谓词

DVCoreDataFinders 提供一个全局过滤谓词,通过 setGlobalFilterPredicate: 指定。如果非 nil,则在执行时将谓词添加到所有查询(作为 and 谓词)。例如,如果您查询谓词 id > 5 并且已设置全局过滤谓词为 isTombstone = NO,则执行的查询是 id > 5 AND isTombstone = NO

所有方法

// counting
+ (NSUInteger)countAllInContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (NSUInteger)countAllWithPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;

// Entity helpers
+ (NSEntityDescription *)entityInContext:(NSManagedObjectContext *)context;

// Finders
+ (NSArray *)findAllInContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (NSArray *)findAllWithFetchRequest:(NSFetchRequest *)fetchRequest inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (NSArray *)findAllWithPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (NSArray *)findAllWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (NSArray *)findAllWithPredicate:(NSPredicate *)predicate sortedBy:(NSString *)sortBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (instancetype)findFirstWithFetchRequest:(NSFetchRequest *)fetchRequest inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (instancetype)findFirstOrInsertWithPredicate:(NSPredicate *)predicate insertBlock:(DVCoreDataFindersCreateBlock)insertBlock inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (instancetype)findFirstWithPredicate:(NSPredicate *)predicate inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (instancetype)findFirstOrInsertWhereProperty:(NSString *)propertyName equals:(id)value insertBlock:(DVCoreDataFindersCreateBlock)insertBlock inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;
+ (instancetype)findFirstWhereProperty:(NSString *)propertyKey equals:(id)value inContext:(NSManagedObjectContext *)context error:(NSError **)errorPtr;

// NSFetchRequests helpers
+ (NSFetchRequest *)fetchRequest;
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate;
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors;
+ (NSFetchRequest *)fetchRequestWithPredicate:(NSPredicate *)predicate sortedBy:(NSString *)sortBy ascending:(BOOL)ascending;

// NSFetchedResultsController helpers
+ (NSFetchedResultsController *)fetchedResultsControllerWithFetchRequest:(NSFetchRequest *)request sectionNameKeyPath:(NSString *)keyPath inContext:(NSManagedObjectContext *)context;
+ (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors inContext:(NSManagedObjectContext *)context;
+ (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)predicate sortedBy:(NSString *)sortedBy ascending:(BOOL)ascending inContext:(NSManagedObjectContext *)context;
+ (NSFetchedResultsController *)fetchedResultsControllerWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors sectionNameKeyPath:(NSString *)keyPath inContext:(NSManagedObjectContext *)context;

// Global filter predicate: if set, the global predicate is automatically added to all queries, all fetch requests, all fetched results controllers, etc.
+ (NSPredicate *)globalFilterPredicate;
+ (void)setGlobalFilterPredicate:(NSPredicate *)predicate;

// NSManagedObject helpers
+ (instancetype)insertIntoContext:(NSManagedObjectContext *)context;

// Instance methods
- (instancetype)findInContext:(NSManagedObjectContext *)context;

联系

Peyman Oreizy @peymano

许可证

DVCoreDataFinders 由 Apache 2.0 许可证提供。有关更多信息,请参阅 LICENSE 文件。