私は以下の表を持っています.isSelectedForSyncフラグがYESになるfileSizeフィールドデータの合計を取得したいと思います.
ここに DigitalLibrary テーブル属性があります
テーブル名: DigitalLibrary
属性とそのタイプ:
- digitalLibraryID (NSNumber)
- ファイル名 (NSString)
- connectType (NSString)
- fileSize (NSNumber - doubleValue)
- isSelectedForSync (NSNumber - BOOL)
- isSync (NSNumber - BOOL)
isSelectedForSync フラグが YES のファイル数を取得したい。また、isSelectedForSync の fileSize の合計を取得する必要があります
NSExpression の下で試していますが、必要なものが得られません。また、エラーが発生します。
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"DigitalLibrary" inManagedObjectContext:self.managedObjectContext];
NSDictionary *entityDict = [entityDescription propertiesByName];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isSync == %@ && isSelectedForSync == %@",[NSNumber numberWithBool:NO], [NSNumber numberWithBool:YES]];
[fetchRequest setPredicate:predicate];
[fetchRequest setEntity:entityDescription];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObjects:[entityDict objectForKey:@"connectType"], nil]];
//AllPhotoCount
NSExpression *countExpression = [NSExpression expressionForFunction:@"count:"
arguments:@[[NSExpression expressionForKeyPath:@"connectType"]]];
NSExpressionDescription *fileCountExpressionDescription = [[NSExpressionDescription alloc] init];
[fileCountExpressionDescription setName:@"filesCount"]; // Choose an appropriate name here!
[fileCountExpressionDescription setExpression:countExpression];
[fileCountExpressionDescription setExpressionResultType:NSInteger32AttributeType];
// //Selected Photo Count
NSExpression *selectedFileExpression = [NSExpression expressionForFunction:@"sum:"
arguments:@[[NSExpression expressionForKeyPath:@"isSelectedForSync"]]];
NSExpressionDescription *selectedFilesExpressionDescription = [[NSExpressionDescription alloc] init];
[selectedFilesExpressionDescription setName:@"selectionCount"]; // Choose an appropriate name here!
[selectedFilesExpressionDescription setExpression:selectedFileExpression];
[selectedFilesExpressionDescription setExpressionResultType:NSInteger32AttributeType];
NSString *shadowVar = @"";
//Sum Of FileSize which file selected
// NSExpression *keyPathSelectedFileSizeTotalExpression = [NSExpression expressionForKeyPath:@"fileSize"];
NSExpression *selectedFileSizeTotalExpression = [NSExpression expressionForFunction:@"sum:"
arguments:@[[NSExpression expressionForKeyPath:@"fileSize"]]];
NSExpression *totalselectedFileSizeExpression = [NSExpression expressionForSubquery:selectedFileSizeTotalExpression usingIteratorVariable:shadowVar predicate:[NSPredicate predicateWithFormat:@"isSelectedForSync == %@",[NSNumber numberWithBool:YES]]];
NSExpressionDescription *selectedFileSizeTotalExpressionDescription = [[NSExpressionDescription alloc] init];
[selectedFileSizeTotalExpressionDescription setName:@"selectedFileSizeTotal"];
[selectedFileSizeTotalExpressionDescription setExpression:totalselectedFileSizeExpression];
[selectedFileSizeTotalExpressionDescription setExpressionResultType:NSInteger64AttributeType];
[fetchRequest setPropertiesToFetch:@[[entityDict objectForKey:@"connectType"], fileCountExpressionDescription, selectedFileSizeTotalExpressionDescription]];
NSLog(@"2nd shadowVar:%@",shadowVar);
NSError* error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
「selectedFileSizeTotalExpressionDescription」を削除すると、上記のコードは完全に機能しましたが、上記のように selectedFileSizeTotalExpressionDescription が必要です。
NSExpression の expressionForSubquery メソッドの使用方法に非常に混乱しています。
以下は、DigitalLibrary テーブルに含まれるデータです。
+-----------+--------------------+--------------+------------+
|fileSize |isSelectedForSync |connectType |fileName |
+-----------+--------------------+--------------+------------+
|1000 |0 |Image |temp1.png |
+-----------+--------------------+--------------+------------+
|200 |1 |Image |tmp.png |
+-----------+--------------------+--------------+------------+
|400 |1 |Image |tmp23.png |
+-----------+--------------------+--------------+------------+
|20000 |1 |Video |test.mp4 |
+-----------+--------------------+--------------+------------+
|3000 |1 |Audio |temp2.mp3 |
+-----------+--------------------+--------------+------------+
|15000 |0 |Video |test12.mp4 |
+-----------+--------------------+--------------+------------+
|3500 |0 |Audio |temp12.mp3 |
+-----------+--------------------+--------------+------------+
以下の結果が欲しい:
+----------------+-------------------+--------------+----------------+------------------+
|TotalFileSize |selectedFileSize |connectType |totalFileCount |selectedFileCount |
+----------------+-------------------+--------------+----------------+------------------+
|1400 |600 |Image |3 |2 |
+----------------+-------------------+--------------+----------------+------------------+
|35000 |20000 |Video |2 |1 |
+----------------+-------------------+--------------+----------------+------------------+
|6500 |3000 |Audio |2 |1 |
+----------------+-------------------+--------------+----------------+------------------+