NSDictionary *productTotalSumAndAveragePriceGroupedByCountries = [[[[[Product all ] aggregatedBy:@[ @[kAggregateSum, @"amount"], @[kAggregatorAverage, @"price"]] ] groupedBy:@[@"country"] ] having:predicate ] execute];
NSFetchRequest *fetchRequest = [[ALFetchRequest alloc] init]; fetchRequest.managedObjectContext = managedObjectContext; NSString *entityName = @"Product"; NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setIncludesPendingChanges:YES]; // sum amount NSExpression *fieldExp1 = [NSExpression expressionForKeyPath:@"amount"]; NSExpression *agrExp1 = [NSExpression expressionForFunction:agr arguments:@[fieldExp1]]; NSExpressionDescription *resultDescription1 = [[NSExpressionDescription alloc] init]; NSString *resultName1 = @"sumAmount"; [resultDescription1 setName:resultName1]; [resultDescription1 setExpression:agrExp1]; [resultDescription1 setExpressionResultType:NSInteger64AttributeType]; // average price NSExpression *fieldExp2 = [NSExpression expressionForKeyPath:@"price"]; NSExpression *agrExp2 = [NSExpression expressionForFunction:agr arguments:@[fieldExp1]]; NSExpressionDescription *resultDescription2 = [[NSExpressionDescription alloc] init]; NSString *resultName2 = @"sumAmount"; [resultDescription2 setName:resultName2]; [resultDescription2 setExpression:agrExp2]; [resultDescription2 setExpressionResultType:NSInteger64AttributeType]; // country NSDictionary *availableKeys = [entity attributesByName]; NSAttributeDescription *country = [availableKeys valueForKey:@"country"]; fetch.propertiesToFetch = [NSArray arrayWithObjects:country, resultDescription1, resultDescription2, nil]; fetch.propertiesToGroupBy = [NSArray arrayWithObject:country]; fetch.resultType = NSDictionaryResultType; NSError *error; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:self error:&error]; if (!fetchedObjects || error) { NSLog(@"Error: Execution of the fetchRequest: %@, Failed with Description: %@",self,error); } return fetchedObjects;
@interface Product : NSManagedObject @property (nonatomic, retain) NSString *title; @property (nonatomic, retain) NSNumber *price; @property (nonatomic, retain) NSNumber *amount; @property (nonatomic, retain) NSString *country; @end
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]; // NSInteger count = [[countProducts firstObject] integerValue]; NSArray *productsOrderedByTitleAndPrice = [[[Product all ] orderedBy:@[ @[@"title", kOrderDESC], @[@"price", kOrderASC], @[@"amount"]] ] execute]; NSArray *totalAmountAndAveragePriceForProducts = [[[[[Product all ] aggregatedBy:@[ @[kAggregateSum, @"amount"], @[kAggregateAverage, @"price"]] ] groupedBy:@[@"country"] ] having:predicate ] execute];
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];
ALTableViewDataSource *dataSource = [[[Product all] orderedBy:@[kTitle, kPrice]] tableViewDataSource]; self.dataSource.tableView = self.tableView;
Product *a = [Product create]; Product *b = [Product createWithDictionary:@{ @"title" : @"best product" }]; // Factory- 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]; //
@implementation Product + (NSString*)entityName { return @"AnItem"; } @end
cd / Users / you / Downloads / ALCoreDataManager-master / Example pod install
- (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; }
[Product createWithFields:@{ kTitle : title, kPrice : @(0), kAmount : @(0) } usingFactory:[ALManagedObjectFactory defaultFactory]];
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 NSDictionaryResultType NSDictionary *d = [result firstObject]; // : // { // sumAmount = 1473; // }
+ (ALFetchRequest*)allInManagedObjectContext:(NSManagedObjectContext*)managedObjectContext; + (ALFetchRequest*)all; // allInManagedObjectContext defaultContext
+ (ALFetchRequest*)allInManagedObjectContext:(NSManagedObjectContext*)managedObjectContext { ALFetchRequest *fetchRequest = [[ALFetchRequest alloc] init]; fetchRequest.managedObjectContext = managedObjectContext; NSEntityDescription *entity = [self entityDescriptionWithMangedObjectContext:managedObjectContext]; [fetchRequest setEntity:entity]; [fetchRequest setIncludesPendingChanges:YES]; return fetchRequest; }
[[[Product all] orderedBy:@[kTitle, kPrice]] limit:1];
- (ALFetchRequest*)limit:(NSInteger)limit { self.fetchLimit = limit; return self; }
- (NSArray*)execute { NSError *error; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:self error:&error]; if (!fetchedObjects || error) { NSLog(@"Error: Execution of the fetchRequest: %@, Failed with Description: %@",self,error); } return fetchedObjects; } - (NSFetchRequest *)request { return (NSFetchRequest*)self; }
Source: https://habr.com/ru/post/265319/
All Articles