Last active
December 18, 2015 00:38
-
-
Save katsuhide/5697489 to your computer and use it in GitHub Desktop.
CoreData周り
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* save | |
*/ | |
-(void)save{ | |
NSError *error = nil; | |
if (![[self managedObjectContext] commitEditing]) { | |
NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd)); | |
} | |
if (![[self managedObjectContext] save:&error]) { | |
[[NSApplication sharedApplication] presentError:error]; | |
} | |
} | |
/* | |
* insert | |
*/ | |
// NSManagedObjectの生成 | |
-(NSManagedObject*)createObject:(NSString*)entity_name{ | |
return [NSEntityDescription insertNewObjectForEntityForName:entity_name inManagedObjectContext:self.managedObjectContext]; | |
} | |
// insertの実行 | |
-(void)insert{ | |
// NSManagedObjectの生成 | |
TaskSource *taskSource = (TaskSource*)[self createObject:@"TaskSource"]; | |
taskSource.task_name = @"task1"; | |
taskSource.interval = [[NSNumber alloc]initWithInt:10]; | |
// Relationがある場合、対象レコードを取得 | |
NSFetchRequest *request = [self createRequest:@"Notebook"]; | |
NSError *error = nil; | |
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:&error]; | |
Notebook *notebook = [result objectAtIndex:0]; | |
taskSource.notebook = notebook; | |
[self save]; | |
} | |
/* | |
* select | |
*/ | |
// fetchRequestの生成 | |
-(NSFetchRequest*)createRequest:(NSString*)entity_name{ | |
return [[NSFetchRequest alloc] initWithEntityName:entity_name]; | |
} | |
// 検索 | |
-(void)select{ | |
// 全件検索 | |
NSFetchRequest *fetchRequest = [self createRequest:@"ENTITY_NAME"]; | |
NSError *error = nil; | |
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; | |
for(TaskSource *taskSource in result) { | |
[taskSource print]; | |
} | |
// 以下、各種条件は設定後にexecuteFetchRequestを実施 | |
// ソート条件の設定 | |
NSSortDescriptor *desc; | |
desc = [[NSSortDescriptor alloc] initWithKey:@"interval" ascending:NO]; | |
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:desc, nil]; | |
[fetchRequest setSortDescriptors:sortDescriptors]; | |
// 取得条件の設定 | |
NSPredicate *pred = [NSPredicate predicateWithFormat:@"task_name = %@", @"task1"]; // 1:key, 2:value | |
[fetchRequest setPredicate:pred]; | |
// 取得最大数の設定 | |
[fetchRequest setFetchBatchSize:10]; | |
[fetchRequest setFetchLimit:1]; // 取得最大数 | |
} | |
// 集計関数検索 : select task_name, sum(interval) from task_source group by task_name; | |
-(void)select_groupby{ | |
// fetchRequestの生成 | |
NSFetchRequest *fetchRequest = [self createRequest:@"ENTITY_NAME"]; | |
//集計の対象を設定します。 | |
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"interval"]; | |
//集計関数(sum)の指定 | |
NSExpression *expression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]]; | |
//集計式の対象(NSExpressionDescription ) | |
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; | |
[expressionDescription setName:@"sumInterval"]; //集計式の名 | |
[expressionDescription setExpression:expression]; //集計関数 | |
[expressionDescription setExpressionResultType:NSInteger32AttributeType]; //結果の種類 | |
[request setPropertiesToGroupBy:[NSArray arrayWithObject:@"task_name"]]; // gourybyのkey | |
[request setPropertiesToFetch:[NSArray arrayWithObjects: expressionDescription, @"task_name", Nil]]; // 出力カラム(group by key) | |
[request setResultType:NSDictionaryResultType]; | |
//結果を取得 | |
NSArray *objs= [self.managedObjectContext executeFetchRequest:request error:nil]; | |
for(int i=0; i < objs.count; i++){ | |
NSArray *array = [objs objectAtIndex:i]; | |
NSLog(@"%@:%@", [array valueForKey:@"task_name"], [array valueForKey:@"sumInterval"]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment