2
私はiPhoneアプリケーションにCore Dataを使用します。 「開始」および「期間」プロパティを持つ「フライト」エンティティがあります。 フライトはページ区切りのビューに表示され、ページあたりの所要時間と期間のロールアップ合計を合計する必要があります。解決策次のネイティブsqliteので 作品:最初のページにあるのでコアデータはsqliteクエリに相当します
select sum(pg.zduration) from (select zduration,zstart from zflight order by zstart limit %i,%i) as pg",offset,limit
- 、5のページサイズで、私は、オフセット= 0と制限= 5で期間和と同じロールアップ期間を取得します。
- 2番目のページでは、期間の合計をoffset = 5、limit = 5としています。オフセット= 0、制限= 10のロールアップ合計。 などなど...今
質問:
私はコアデータ、NSExpression、NSExpressionDescriptionとNSFetchRequestではなく、sqliteのことを解決する方法は?あなたが言ったように、
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;