strftime SQL function in Core data - ios

I'm trying to make a fetch in Core Data where I group by month of my property date.
In SQL
SELECT SUM(total) as value,
date as date,
strftime('%m-%Y', date) as convertDate
FROM table
GROUP BY convertDate
I don't see any function where I could use strftime.
Is there a way to do this with a NSFetchRequest instead of a NSFetchedResultController?

you can do this by using NSExpressionDescription
for the grouped by With NSFetchRequest you can see this tutorial or this stackOverFlow question
For the sum here the NSExpressionDescription to add to your NSFetchRequest
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
expressionDescription.name = #"sumOfAmounts";
expressionDescription.expression = [NSExpression expressionForKeyPath:#"#sum.total"];
expressionDescription.expressionResultType = NSDecimalAttributeType;
For the date i think that you can format it by using NSDateFormatter when you get fetch records.
Let me know if this was helpful :)
UPDATE
[fetch setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc, expressionDescription, nil]];
[fetch setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
[fetch setResultType:NSDictionaryResultType];
NSError* error = nil;
NSArray *results = [myManagedObjectContext executeFetchRequest:fetch
error:&error];

Related

Query max NSDate in core data

Is there a way to query NSDate in CoreData. For example if I want an entity with the highest NSDate value? I see that NSExpression "max:" only takes an NSNumber.
You can actually ask SQL for just that value, not the object with that value:
NSExpression *date = [NSExpression expressionForKeyPath:#"date"];
NSExpression *maxDate = [NSExpression expressionForFunction:#"max:"
arguments:[NSArray arrayWithObject:maxDate]];
NSExpressionDescription *d = [[[NSExpressionDescription alloc] init] autorelease];
[d setName:#"maxDate"];
[d setExpression:maxSalaryExpression];
[d setExpressionResultType:NSDateAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:d]];
NSError *error = nil;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
// Handle the error.
} else {
if (0 < [objects count]) {
NSLog(#"Maximum date: %#", [[objects objectAtIndex:0] valueForKey:#"maxDate"]);
}
}
more detail under Fetching Managed Objects -> Fetching Specific Values in the CoreData documentation.
or
Perfomed a query, ordered on Date field DESCENDING, and using setFetchLim it:1.
Its not perfect, but at least it worked.
You can do this directly in SQLite-- without fetching everything and then filtering the result, and without the complexity of NSExpression.
To get the one object that has the max date, do something like (assuming entity name Entity and date attribute timeStamp):
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Event"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"timeStamp = self.#max.timeStamp"];
fetchRequest.predicate = predicate;
Do the fetch. You'll get (at most) one result, which will be the instance with the max date.
If you want to get just the date and not the entire managed object, add this before doing the fetch:
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = #[ #"timeStamp" ];
you can get it with
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:#"tablename"];
fetchRequest.fetchLimit = 1;
fetchRequest.sortDescriptors = #[[NSSortDescriptor sortDescriptorWithKey:#"yourDate" ascending:NO]];
NSError *error = nil;
id person = [managedObjectContext executeFetchRequest:fetchRequest error:&error].firstObject;

Sum of a column for distinct id using core data

I want to get the sum of all values in a column for a distinct id(date in my case). My code is
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:#"Table"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(endTime!=%#)",nil];
[fetchRequest setPredicate:predicate];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.returnsDistinctResults=YES;
NSExpressionDescription *aDescription = [[NSExpressionDescription alloc] init];
aDescription.name = #"A";
aDescription.expression = [NSExpression expressionForKeyPath:#"#sum.a"];
aDescription.expressionResultType = NSDecimalAttributeType;
NSExpressionDescription *bDescription = [[NSExpressionDescription alloc] init];
bDescription.name = #"B";
bDescription.expression = [NSExpression expressionForKeyPath:#"#sum.b"];
bDescription.expressionResultType = NSDecimalAttributeType;
NSArray *properties = [NSArray arrayWithObjects:#"date",aDescription, bDescription, nil];
[fetchRequest setPropertiesToFetch:properties];
NSArray *result = [temporaryContext executeFetchRequest:fetchRequest error:&error];
NSLog(#"Array result is : %#",result);
When i run the above query i get sum of column a, column b and distinct dates. The problem is it will not add up the sum for a distinct date, instead it will add up all the values in the column a and b.
In simple words i want to get results as in this question Link but in core data. Any help would be greatly appreciated.
Try setting the fetch request to group:
[fetchRequest setPropertiesToGroupBy:#[ #"date" ]];

how to group by day with core data?

I have a Entity called deal and deal has a property called date which is the time this deal object inserted into the store.
and one day may have several deals.
So I want count some data group by day, I want fetch dayand countofsomething
like:
2013-06-03 3
2013-06-02 4
and I don't want to use sectionPath because it only put deals into section.
I know I can have this done by have another property(type:string) like dayOfTheDate which is like 2013-06-03 in each object.
btw, transient property don't seem to work in this situation
Could you understand what I am looking for?
Comment here so I can provide more detail
Thanks all of you.
Sample of how I did it when I counted number of same notes
NSEntityDescription* entity = [NSEntityDescription entityForName:#"Assets"
inManagedObjectContext:[appDelegate managedObjectContext]];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:#"notes"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: #"assetUrl"]; // Does not really matter
NSExpression *countExpression = [NSExpression expressionForFunction: #"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: #"count"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[searchFetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc,expressionDescription, nil]];
[searchFetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"timestamp" ascending:NO];
[searchFetchRequest setSortDescriptors:#[sortDescriptor]];
[searchFetchRequest setFetchLimit:10];
NSPredicate *query = [NSPredicate predicateWithFormat:#"notes contains[cd] %#",_txtCameraNote.text];
[searchFetchRequest setPredicate:query];
[searchFetchRequest setResultType:NSDictionaryResultType];
NSArray *fetchedObjects = [appContext executeFetchRequest:searchFetchRequest error:nil];
fetchedObjects would be something like this.
({
count = 1;
notes = "glenny and me";
},
{
count = 6;
notes = macair;
})

How do you construct the predicate for NSFetchRequest setHavingPredicate:?

For the life of me I can not seem to get this to work.
Assume our entity is an managed object with a status field and an order field.
How would I go about getting all orderedEntries having more than one order that are the same?
Please no answers telling me to just do a subquery with #count in the main predicate, since I know of that solution, the point of this post is to understand how to use the having predicate in core data, which would probably be faster than a subquery anyways. (unless you explain why I can not use a having clause)
The following code would return an array of dictionaries with the number of orders per order number. What I want is to be able to add a having clause to restrict my request to only return the dictionaries representing objects of those orders that have a count greater than 1.
Here is the code so far and my attempts at a having predicate:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"OrderedEntry"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(status == %#)",[NSNumber numberWithInt:EntryStatusAlive]];
[fetchRequest setPredicate:predicate];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: #"order"]; // Does not really matter
NSExpression *maxExpression = [NSExpression expressionForFunction: #"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: #"orderCount"];
[expressionDescription setExpression: maxExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[fetchRequest setPropertiesToFetch: [NSArray arrayWithObjects:expressionDescription,#"order",nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:#"order",nil]];
//[fetchRequest setHavingPredicate:[NSPredicate predicateWithFormat:#"#count > 1"]];
//[fetchRequest setHavingPredicate:[NSComparisonPredicate predicateWithLeftExpression:maxExpression rightExpression:[NSExpression expressionForConstantValue:[NSNumber numberWithInteger:1]] modifier:NSDirectPredicateModifier type:NSGreaterThanPredicateOperatorType options:NSCaseInsensitivePredicateOption]];
NSError *error;
NSArray * array = [context executeFetchRequest:fetchRequest error:&error];
I ended up going with this for anyone interested
-(BOOL)ordersAreSaneOnDay:(NSNumber*)dayNumber forUser:(User*)user inContext:(NSManagedObjectContext*)context {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"BasicEntry"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setResultType:NSDictionaryResultType];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
#"(status == %#) && ((type != %#) && (type != %#) && (dayNumber == %#)) && ((user == NIL) || (user == %#))",[NSNumber numberWithInt:EntryStatusAlive],[NSNumber numberWithInt:EntryTypeTask],[NSNumber numberWithInt:EntryTypeCompletedTask],dayNumber,user];
[fetchRequest setPredicate:predicate];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: #"order"]; // Does not really matter
NSExpression *maxExpression = [NSExpression expressionForFunction: #"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: #"orderCount"];
[expressionDescription setExpression: maxExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[fetchRequest setPropertiesToFetch: [NSArray arrayWithObjects:expressionDescription,#"order",nil]];
[expressionDescription release];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:#"order",nil]];
//[fetchRequest setHavingPredicate:[NSPredicate predicateWithFormat:#"self.order.#count > 1"]];
//[fetchRequest setHavingPredicate:[NSComparisonPredicate predicateWithLeftExpression:maxExpression rightExpression:[NSExpression expressionForConstantValue:[NSNumber numberWithInteger:1]] modifier:NSDirectPredicateModifier type:NSGreaterThanPredicateOperatorType options:NSCaseInsensitivePredicateOption]];
NSError *error;
NSArray * array = [context executeFetchRequest:fetchRequest error:&error];
array = [array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"orderCount > 1"]];
//NSLog(#"it worked %#",array);
[fetchRequest release];
if ([array count]) return FALSE;
return TRUE;
}
I've got this working using the following:
[fetchRequest setHavingPredicate:[NSPredicate predicateWithFormat:#"$orderCount > 1"]];
Use the name of your expressionDecription as variable $orderCount.
Alternatively you can use
NSExpression *countExpression = [NSExpression expressionForVariable:#"orderCount"];
[fetchRequest setHavingPredicate:[NSPredicate predicateWithFormat:#"%# > 1", countExpression]];
Firstly, whenever I try something analogous to what you're doing, I get an error that you can't pass a to-many relationship to setPropertiesToFetch:. The NSFetchRequest documentation backs this up: "The property descriptions may represent attributes, to-one relationships, or expressions." So that's problem #1.
Problem #2 is that it appears that you can't group by a to-many relationship either (this isn't made clear in the documentation, but you get the same error and it also makes sense).
Remove "order" from the properties to fetch. Group by an attribute. Modify your main predicate to only include those attributes you group by (or just remove it). Specify "order" in your having predicate.
[fetchRequest setPropertiesToGroupBy: #[ #"???" ]];
[fetchRequest setHavingPredicate: [NSPredicate predicateWithFormat: #"self.order.#count > 1"]];
Now you'll see the request will work, but the results probably weren't what you were expecting:
- NSArray
- NSDictionary { status = #"alive", orderCount = "4" }
- NSDictionary { status = #"alive", orderCount = "9" }
- NSDictionary { status = #"alive", orderCount = "2" }
- etc...
NSDictionaryResultType doesn't actually give you anything to identify those objects by - it just gives you the values.
So your next step is to get back IDs for those OrderedEntry objects. The trick is to include an expression which will give you back the NSManagedObjectID as a key in each dictionary.
I don't know if this will actually give you improved performance at all (over just AND-ing it in to the main predicate). In my experience, one of the best things you can do to improve fetching performance is to create singleton NSPredicates and use substitution variables to set up each fetch. Predicate parsing can be expensive.
Dictionary result types can be a pain. Usually just constructing a good predicate is better. I tend only to use them for expressions (i.e. performing statistic-type calculations on the graph which return a value). If you look at all the restrictions around properties to fetch and group by and the predicate restrictions, this seems to be what Apple intend it for. I'm not even sure it's possible to do what you want to do by grouping and using a having predicate - for example, what are you going to group by? If by status (which you need to group by to include in your predicate), won't that collapse all the OrderEntries and you won't get the separate objectIDs and orderCounts? It's best to stick to the main predicate for this, not grouping and having predicates.

Core Data equivalent for sqlite query

I use Core Data for an iPhone app.
There is "Flight" entity with a "start" and "duration" property.
The flights are listed on a paginated view, where I need to sum the duration per page and the duration rollup sum.
In native sqlite following solution works:
select sum(pg.zduration) from (select zduration,zstart from zflight order by zstart limit %i,%i) as pg",offset,limit
So on first page, with a page size of 5, I get duration sum and same rollup duration with offset=0 and limit=5.
On second page, I get the duration sum with offset=5 and limit=5. The rollup sum with offset=0 and limit=10.
And so on..
Now the Question:
How would I solve that with Core Data, NSExpression, NSExpressionDescription and NSFetchRequest instead of sqlite? Of course, I would not like to load all flight objects in memory...
So I am able to caculate the duration for all flights:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Flight" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
NSSortDescriptor *startSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"start"
ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:startSortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
request.fetchOffset=onPage*pageSize;//does not help, cause offset and limit are applied to the result
request.fetchLimit=pageSize;//does not help, cause offset and limit are applied to the result
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:#"duration"];
NSExpression *sumExpression = [NSExpression expressionForFunction:#"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];
// Create an expression description using the minExpression and returning a date.
NSExpressionDescription *expressionDescription1 = [[NSExpressionDescription alloc] init];
[expressionDescription1 setName:#"durationSum"];
[expressionDescription1 setExpression:sumExpression];
[expressionDescription1 setExpressionResultType:NSInteger64AttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObjects:expressionDescription1,nil]];
// Execute the fetch.
NSError *error = nil;
NSArray *objects = [self.managedObjectContext executeFetchRequest:request error:&error];
if(error!=nil){
[NSException raise:#"Sum Page Duration failed" format:#"%# Error:%#", [[error userInfo] valueForKey:#"reason"],error];
}
if (objects!=nil && [objects count] > 0) {
return (NSNumber*)[[objects objectAtIndex:0] valueForKey:#"durationSum"];
}
return 0;
As you said, the limit and offset set on the fetch request are applied to the result and NSExpression won't work well in this case. You could operate on the returned objects, after they've been offset and limited by the fetch request, using a collection operator rather than NSExpression, e.g.:
NSNumber *durationSum = [objects valueForKeyPath:#"#sum.duration"];

Resources