{"id":264145,"date":"2015-08-24T12:45:02","date_gmt":"2015-08-24T08:45:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=264145"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=264145","title":{"rendered":"\u0417\u0430\u043f\u0440\u043e\u0441\u044b \u0432 CoreData \u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 \u0438 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443"},"content":{"rendered":"<p>             \u041f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u043c, \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 CoreData, \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441 <i>\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438<\/i> \u0438 <i>\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438<\/i>. \u041e\u0434\u043d\u0430\u043a\u043e, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 CoreData \u0441\u043b\u043e\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043d\u0435\u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043d\u043e \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u0435\u043d. <\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager#query-builder\">\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/a> \u043c\u044b \u043c\u043e\u0436\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:  <\/p>\n<pre><code class=\"objectivec\">NSDictionary *productTotalSumAndAveragePriceGroupedByCountries =  [[[[[Product all ] aggregatedBy:@[                  @[kAggregateSum, @&quot;amount&quot;],                  @[kAggregatorAverage, @&quot;price&quot;]] ] groupedBy:@[@&quot;country&quot;] ] having:predicate ] execute]; <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u043d\u0435 \u0431\u044b \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 <a href=\"http:\/\/http\u044b:\/\/github.com\/appleios\/ALCoreDataManager\">\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435<\/a> \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 CoreData, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u043a\u0430\u043a \u043e\u0431\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u043c\u043e\u0435\u0433\u043e \u0441\u043a\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0434 iOS. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0432 <a href=\"https:\/\/cocoapods.org\/pods\/ALCoreDataManager\">cocoapods<\/a>.<\/p>\n<p>  \u042f\u0434\u0440\u043e\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0438\u043d\u0433\u043b\u0442\u043e\u043d \u043a\u043b\u0430\u0441\u0441 ALCoreDataManager, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u044d\u043a\u0430 CoreData \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 NSManagedObjectContext. \u0417\u0430\u043c\u0435\u0447\u0443, \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043e\u0440\u0434\u0438\u043d\u0430\u0440\u043d\u0430\u044f \u0432\u0435\u0449\u044c \u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u043f\u043b\u0430\u043d\u0435 \u2014 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e. \u0412\u0441\u0435 \u0432\u043a\u0443\u0441\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u0432 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 ALFetchRequest+QueryBuilder \u0438 \u0444\u0430\u043a\u0442\u043e\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0435 ALManagedObjectFactory. \u041d\u043e \u043e\u0431\u043e \u0432\u0441\u0435\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<h2>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/h2>\n<p>  \u0411\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0430\u0433\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u043e\u0434\u0435\u043b\u044c Product \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"objectivec\">@interface Product : NSManagedObject @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) NSNumber *price; @property (nonatomic, retain) NSNumber *amount; @property (nonatomic, retain) NSString *country; @end <\/code><\/pre>\n<p>  <\/p>\n<h3>\u0417\u0430\u043f\u0440\u043e\u0441\u044b<\/h3>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager#query-builder\">\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/a>, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b:  <\/p>\n<pre><code class=\"objectivec\">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:@[@&quot;title&quot;]] distinct] execute];  NSArray *countProducts = [[[[Product all] where:predicate] count] execute]; \/\/ NSInteger count = [[countProducts firstObject] integerValue];  NSArray *productsOrderedByTitleAndPrice =  [[[Product all ] orderedBy:@[               @[@&quot;title&quot;, kOrderDESC],               @[@&quot;price&quot;, kOrderASC],               @[@&quot;amount&quot;]] ] execute];  NSArray *totalAmountAndAveragePriceForProducts =  [[[[[Product all ] aggregatedBy:@[                  @[kAggregateSum, @&quot;amount&quot;],                  @[kAggregateAverage, @&quot;price&quot;]] ] groupedBy:@[@&quot;country&quot;] ] having:predicate ] execute]; <\/code><\/pre>\n<p>  \u041c\u0435\u0442\u043e\u0434 <b>execute<\/b> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e NSFetchRequest \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d \u043c\u0435\u0442\u043e\u0434 <b>request<\/b>. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440,  <\/p>\n<pre><code class=\"objectivec\">NSFetchRequest *request = [[[Product all] orderedBy:@[@&quot;title&quot;, @&quot;price&quot;]] request]; NSManagedObjectContext *context = [ALCoreDataManager defaultManager].managedObjectContext; NSFetchedResultsController *controller = [[NSFetchedResultsController alloc] initWithFetchRequest:request                                     managedObjectContext:context                                       sectionNameKeyPath:nil                                                cacheName:nil]; [controller performFetch:nil]; <\/code><\/pre>\n<p>  <\/p>\n<h3>UITableViewDataSource<\/h3>\n<p>  \u041f\u043e \u0438\u043c\u0435\u044e\u0449\u0435\u043c\u0443\u0441\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u044c\u0435\u043a\u0442, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b UITableViewDataSource \u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u044f\u0435\u043c\u044b\u0439 <a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/CoreData\/Reference\/NSFetchedResultsControllerDelegate_Protocol\/\">NSFetchedResultsController<\/a>\u043e\u043c:  <\/p>\n<pre><code class=\"objectivec\">ALTableViewDataSource *dataSource =         [[[Product all] orderedBy:@[kTitle, kPrice]] tableViewDataSource];  self.dataSource.tableView = self.tableView; <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0438\u0437\u0431\u0430\u0432\u0438\u0442 \u043d\u0430\u0441 \u043e\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u043a\u0443\u0447\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0430 UITableViewDataSource \u0438 \u0434\u0435\u043b\u0435\u0433\u0430\u0442\u0430 \u0434\u043b\u044f NSFetchedResultsController-\u0430. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u044f <a href=\"https:\/\/github.com\/appleios\/ALDataSource\">UICollectionViewDataSource<\/a> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 <b>collectionViewDataSource<\/b>.<\/p>\n<h3>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432<\/h3>\n<p>  \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044c\u0435\u043a\u0442\u043e\u0432 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0442\u0430\u043a\u043e\u0433\u043e <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager#create-objects\">API<\/a>:  <\/p>\n<pre><code class=\"objectivec\">Product *a = [Product create]; Product *b = [Product createWithDictionary:@{ @&quot;title&quot; : @&quot;best product&quot; }];  \/\/ \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Factory-\u043a\u043b\u0430\u0441\u0441 NSManagedObjectContext *context = [ALCoreDataManager defaultManager].managedObjectContext; ALManagedObjectFactory *factory = [[ALManagedObjectFactory alloc] initWithManagedObjectContext:context];  Product *c = [Product createWithDictionary:nil                                usingFactory:factory]; c.title = @&quot;best product 2&quot;;  Product *d = [Product createWithDictionary:@{ @&quot;title&quot; : @&quot;best product 3&quot;, @&quot;price&quot; : @(100) }                                usingFactory:factory];  [d remove]; \/\/ \u0443\u0434\u0430\u043b\u044f\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442 <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u2014 \u0432\u044b \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u043f\u0435\u0440\u0435\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434 <b>+entityName<\/b>, \u0435\u0441\u043b\u0438 <i>Entity Name<\/i> \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e ManagedObject-\u0430 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0435\u0433\u043e <i>Class Name<\/i> (\u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438).  <\/p>\n<pre><code class=\"objectivec\">@implementation Product + (NSString*)entityName {     return @&quot;AnItem&quot;; } @end <\/code><\/pre>\n<h2>Example<\/h2>\n<p>  \u041f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u043e\u0444\u0438\u0442 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager\/tree\/master\/Example\">\u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435<\/a>. \u041f\u043e\u0441\u043b\u0435 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0430\u0437\u0430\u0440\u0445\u0438\u0432\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:  <\/p>\n<pre> cd \/Users\/you\/Downloads\/ALCoreDataManager-master\/Example pod install <\/pre>\n<p>  \u0412 Storyboard-\u0435 \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/6ad\/d77\/9a3\/6add779a345c488490e2e216b4449239.png\"\/><br \/>  \u0412 \u043f\u0435\u0440\u0432\u043e\u043c TableViewController-\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0441\u0435\u0445 Product-\u043e\u0432; \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043f\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u043c\u0443 Product-\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043c \u0436\u0435 \u043e\u0442\u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u0432\u0448\u0435\u0433\u043e\u0441\u044f \u0440\u0430\u043d\u0435\u0435 ALTableViewDataSource-\u0430:  <\/p>\n<pre><code class=\"objectivec\">- (void)viewDidLoad {     [super viewDidLoad];          self.dataSource = [[[Product all] orderedBy:@[kTitle, kPrice]] tableViewDataSource];     __weak typeof(self) weakSelf = self;     self.dataSource.cellConfigurationBlock = ^(UITableViewCell *cell, NSIndexPath *indexPath){         [weakSelf configureCell:cell atIndexPath:indexPath];     };     self.dataSource.reuseIdentifierBlock = ^(NSIndexPath *indexPath){         return TableViewCellReuseIdentifier;     };     self.dataSource.tableView = self.tableView; } <\/code><\/pre>\n<p>  \u041f\u043e \u043d\u0430\u0436\u0430\u0442\u0438\u044e \u043d\u0430 <b>Add<\/b>, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0441\u043a\u0430\u0437\u0430\u0432:  <\/p>\n<pre><code class=\"objectivec\">[Product createWithFields:@{                             kTitle : title,                             kPrice : @(0),                             kAmount : @(0)                             }              usingFactory:[ALManagedObjectFactory defaultFactory]]; <\/code><\/pre>\n<p>  \u041f\u043e\u043a\u0430\u0436\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u043e \u043d\u0430\u0436\u0430\u0442\u0438\u044e \u043d\u0430 <b>Statistics<\/b>:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/b2f\/830\/70c\/b2f83070c125459186305a278f0cf026.png\"\/><\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u0442\u0438\u043f\u0430 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438 \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u0434:  <\/p>\n<pre><code class=\"objectivec\">ALFetchRequest *request = nil; switch (st) {     case ALStatsTypeTotalAmount:         request = [[Product all] aggregatedBy:@[@[kAggregatorSum, kAmount]]];         break;     case ALStatsTypeMedianPrice:         request = [[Product all] aggregatedBy:@[@[kAggregatorAverage, kPrice]]];         break;     default:         break; } NSArray *result = [request execute]; \/\/ request \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0442\u0438\u043f NSDictionaryResultType NSDictionary *d = [result firstObject]; \/\/ \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: \/\/ { \/\/    sumAmount = 1473; \/\/ } <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0435\u0441\u044c \u043a\u043e\u0434 \u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438 (\u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u2014 <a href=\"http:\/\/stackoverflow.com\/questions\/23525565\/core-data-comparisons-within-aggregate-functions\">request \u0441 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 (stackoverflow)<\/a>).<\/p>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u0430\u043a\u043e\u0439 AlertView:<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/20f\/620\/193\/20f620193ca5414a8518fc6c0d3d2b22.png\" height=\"620\"\/><\/p>\n<h2>\u041a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442<\/h2>\n<p>  \u0424\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0432\u044b\u0437\u043e\u0432\u0430:  <\/p>\n<pre><code class=\"objectivec\">+ (ALFetchRequest*)allInManagedObjectContext:(NSManagedObjectContext*)managedObjectContext; + (ALFetchRequest*)all; \/\/ \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u044b\u0437\u043e\u0432 allInManagedObjectContext \u0441 defaultContext <\/code><\/pre>\n<p>  \u0422\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e NSFetchRequest-\u0430:  <\/p>\n<pre><code class=\"objectivec\">+ (ALFetchRequest*)allInManagedObjectContext:(NSManagedObjectContext*)managedObjectContext { \tALFetchRequest *fetchRequest = [[ALFetchRequest alloc] init]; \tfetchRequest.managedObjectContext = managedObjectContext; \tNSEntityDescription *entity = [self entityDescriptionWithMangedObjectContext:managedObjectContext];  \t[fetchRequest setEntity:entity]; \t[fetchRequest setIncludesPendingChanges:YES];  \treturn fetchRequest; } <\/code><\/pre>\n<p>  \u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0435\u0441\u044c \u043a\u043e\u0434 builder-\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager\/blob\/master\/Pod\/Classes\/ALFetchRequest%2BQueryBuilder.m\">ALFetchRequest+QueryBuilder.m<\/a>.<\/p>\n<p>  \u041a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0432\u0438\u0434\u0430  <\/p>\n<pre><code class=\"objectivec\">[[[Product all] orderedBy:@[kTitle, kPrice]] limit:1]; <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0432 \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 NSFetchRequest, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:  <\/p>\n<pre><code class=\"objectivec\">- (ALFetchRequest*)limit:(NSInteger)limit { \tself.fetchLimit = limit; \treturn self; } <\/code><\/pre>\n<p>  \u041c\u043e\u0442\u043e\u0434\u044b <b>execute<\/b> \u0438 <b>request<\/b>:  <\/p>\n<pre><code class=\"objectivec\">- (NSArray*)execute { \tNSError *error; \tNSManagedObjectContext *managedObjectContext = self.managedObjectContext; \tNSArray *fetchedObjects = [managedObjectContext executeFetchRequest:self error:&error]; \tif (!fetchedObjects || error) { \t\tNSLog(@&quot;Error: Execution of the fetchRequest: %@, Failed with Description: %@&quot;,self,error); \t} \treturn fetchedObjects; } - (NSFetchRequest *)request { \treturn (NSFetchRequest*)self; } <\/code><\/pre>\n<p>  \u041c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e <i>syntactic sugar<\/i> \u0434\u043b\u044f NSFetchRequest. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, overhead \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0432\u0435\u043d \u043d\u0443\u043b\u044e. \u0427\u0443\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager\/blob\/master\/Example\/Tests\/Tests.m\">\u0442\u0435\u0441\u0442\u0430\u0445<\/a>.<\/p>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0432\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043f\u0435\u0448\u0443 \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u0442\u044c. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435.             <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/265319\/\"> http:\/\/habrahabr.ru\/post\/265319\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u041f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u043c, \u043a\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 CoreData, \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0441 <i>\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438<\/i> \u0438 <i>\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438<\/i>. \u041e\u0434\u043d\u0430\u043a\u043e, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0432 CoreData \u0441\u043b\u043e\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0438 \u043d\u0435\u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043d\u043e \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u0435\u043d. <\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"https:\/\/github.com\/appleios\/ALCoreDataManager#query-builder\">\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/a> \u043c\u044b \u043c\u043e\u0436\u0435\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441:  <\/p>\n<pre><code class=\"objectivec\">NSDictionary *productTotalSumAndAveragePriceGroupedByCountries =  [[[[[Product all ] aggregatedBy:@[                  @[kAggregateSum, @&quot;amount&quot;],                  @[kAggregatorAverage, @&quot;price&quot;]] ] groupedBy:@[@&quot;country&quot;] ] having:predicate ] execute]; <\/code><\/pre>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-264145","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/264145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264145"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/264145\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}