ALCoreDataManager 0.2.3

ALCoreDataManager 0.2.3

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最新发布Sep 2015

Aziz U. Latypov 维护。



  • Aziz U. Latypov

用法

导入头文件

#import <ALCoreDataManager/ALCoreData.h>

为了在应用终止时保存上下文,将以下代码添加到您的 AppDelegate

- (void)applicationWillTerminate:(UIApplication *)application
{
    [[ALCoreDataManager defaultManager] saveContext];
}

要获取 NSManagedObjectContext,使用

[ALCoreDataManager defaultManager].managedObjectContext

创建对象

您可以使用简单的创建方法来创建托管对象

Product *a = [Product create];
Product *b = [Product createWithDictionary:@{ @"title" : @"best product" }];

// or using Factory class
NSManagedObjectContext *context = [ALCoreDataManager defaultManager].managedObjectContext;
ALManagedObjectFactory *factory =
[[ALManagedObjectFactory alloc] initWithManagedObjectContext:context];

Product *c = [Product createWithDictionary:nil 
                              usingFactory:factory];
c.title = @"best product 2";

Product *d = [Product createWithDictionary:@{ @"title" : @"best product 3", @"price" : @(100) } 
                              usingFactory:factory];

[d remove]; // remove an object

查询构建器

NSArray *allProducts = 
[[Product all] execute];

NSArray *productsFilteredWithPredicate = 
[[[Product all] where:predicate] execute];

NSArray *singleProduct = 
[[[[Product all] where:predicate] limit:1] execute];

NSArray *onlyDistinctProductTitles = 
[[[[Product all] properties:@[@"title"]] distinct] execute];

NSArray *countProducts =
[[[[Product all] where:predicate] count] execute];

NSArray *orderedItems = 
[[[Product all
] orderedBy:@[
              @[@"title", kOrderDESC],
              @[@"price", kOrderASC],
              @[@"amount"]]
] execute];

NSArray *aggregatedItems = 
[[[[[Product all
] aggregatedBy:@[
                 @[kAggregateSum, @"amount"],
                 @[kAggregateMedian, @"price"]]
] groupedBy:@[@"country"]
] having:predicate
] execute];

对于 orderedBy:,您可以省略排序 ASC/DESC - 默认排序为 ASC。

可用的聚合包括

  • kAggregateSum
  • kAggregateCount
  • kAggregateMin
  • kAggregateMax
  • kAggregateAverage
  • kAggregateMedian

您也可以获取请求

NSFetchRequest *request = [[[Product all] orderedBy:@[@"title", @"price"]] request];
NSManagedObjectContext *context = [ALCoreDataManager defaultManager].managedObjectContext;
NSFetchedResultsController *controller =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
                                    managedObjectContext:context
                                      sectionNameKeyPath:nil
                                               cacheName:nil];
[controller performFetch:nil];

并发

[[ALCoreDataManager defaultManager] saveAfterPerformingBlock:^(NSManagedObjectContext *localContext)
{
  NSArray *remoteUpdates = ...;

  ALManagedObjectFactory *factory = [[ALManagedObjectFactory alloc] initWithManagedObjectContext:localContext];

  for(NSDictionary *d in remoteUpdates){
    [Item createWithDictionary:d 
                  usingFactory:factory];
  }

} withCompletionHandler:^{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"FetchingUpdatesDone" 
                                                      object:nil];
}];

这将自动将更改保存到 localContext 并与默认的 defaultContext 合并,完成后发出通知。

限制

根据文档中的

  • (void)setIncludesPendingChanges:(BOOL)yesNo

值 YES 不可与结果类型 NSDictionaryResultType 一同使用,包括聚合结果(例如最大值和最小值)的计算。对于字典,从获取返回的数组反映了持久存储中的当前状态,不考虑上下文中任何挂起的更改,插入或删除。如果需要将挂起的更改考虑在内进行某些简单的聚合(如最大值和最小值),您可以使用按您想要排序的属性的普通获取请求,并限制获取数量为 1。

因此使用 aggregatedBy/groupedBy/having 将忽略 未保存的数据。

要求

如果您的实体名称与其类名不同,则您必须覆盖方法 +entityName

@implementation Item

+ (NSString*)entityName
{
    return @"AnItem";
}

@end

安装

作者

Aziz U. Latypov, [email protected]

许可

ALCoreDataManager 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。