cocoa - NSFetchRequest with distinct values and expression -



cocoa - NSFetchRequest with distinct values and expression -

i have create nsfetchrequest iphone app returns same results next sql statement:

select week , year , sum(duration) totalduration mytable grouping year , week

i've tried solve next code:

nsexpression * durationexpression = [nsexpression expressionforfunction:@"sum:" arguments:[nsarray arraywithobject:[nsexpression expressionforkeypath:@"duration"]]]; nsexpressiondescription * durationexpressiondescription = [[[nsexpressiondescription alloc] init] autorelease]; [durationexpressiondescription setexpression:durationexpression]; [durationexpressiondescription setexpressionresulttype:nsdoubleattributetype]; [durationexpressiondescription setname:@"totalduration"]; nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; nsentitydescription *entity = [nsentitydescription entityforname:@"myentity" inmanagedobjectcontext:managedobjectcontext]; nsarray *propertiestofetch = [[nsarray alloc] initwithobjects:@"year", @"week", durationexpressiondescription, nil]; [fetchrequest setentity:entity]; [fetchrequest setreturnsdistinctresults:yes]; [fetchrequest setresulttype:nsdictionaryresulttype]; [fetchrequest setpropertiestofetch:propertiestofetch];

but grouping results 1 single row. hints how solve it?

edit: source table has next data:

year | month | duration 2011 | 7 | 10 2011 | 7 | 15 2011 | 6 | 15 2011 | 5 | 10

the sql statement returns right result i'd achieve:

year | month | duration 2011 | 7 | 25 2011 | 6 | 15 2011 | 5 | 10

the nsfetchrequest returns:

year | month | duration 2011 | 7 | 50

pretty sure single homecoming triggered nsexpression can homecoming single value. otherwise, array of dictionaries this:

year | month | duration 2011 | 7 | 50 2011 | 6 | 50 2011 | 5 | 50

... wouldn't reflect state of data.

the look configured homecoming sum of duration values in every instance of myentity given it. since have no fetch predicate, given every existing instance of myentity available context. nevertheless, can homecoming single value , means single dictionary when fetching property.

the best solution create separate fetch look since has nil other values beingness fetched i.e. summed value isn't associated particular year or month value. in case, simple fetches run faster separating look out give overall speed boost through have 2 fetches instead of one.

i caution avoid trying utilize core info sql. cardinal error think of core info sql wrapper. core info not sql. entities not tables. objects not rows. attributes not columns. relationships not joins. core info object graph management scheme may or may not persist object graph , may or may not utilize sql far behind scenes so. there surprisingly little translates straight sql core data.

cocoa core-data

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -