在我的示例应用程序中,我有两个实体调用帐户和客户
对于那个实体,我创建了两个NSManagedObject子类
core data entities http://i.stack.
imgur.com/zmt1N.png
现在我完全不知道它是正确的还是什么,我必须保存到这些核心数据实体并检索详细信息,例如“在帐户表中拥有三个以上帐户的客户 “
希望得到一些详细的解释。在此先感谢答案 0 :(得分:2)
使用NSPredicate
使用不区分大小写的字符串“搜索”名称的简单示例:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"customer"];
request.predicate = [NSPredicate predicateWithFormat:@"name LIKE[c] %@", @"Amazon"];
NSArray *matches = [self.managedObjectContext executeFetchRequest:request error:nil];
根据上面链接的NSPredicate Class Reference:
您可以为关系创建谓词,例如:
group.name喜欢“work *”
所有children.age> 12
任何children.age> 12
答案 1 :(得分:0)
这是我期望做的。谢谢你的帮助
-(void)savingDummyDatas
{
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
//execution starting time
NSDate *methodStart = [NSDate date];
//
if ([objects count] == 0)
{
#pragma mark - Entity Insertion With Relation
//for (int i=1; i<=100000; i++) {
//1
Customer *custObj1 = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:context];
custObj1.name = @"Yeswant";
// add accoutns for customer
Accounts *acObj1 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj1.accountNumber = @"001";
[custObj1 addCust_relationObject:acObj1];
Accounts *acObj2 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj2.accountNumber = @"002";
[custObj1 addCust_relationObject:acObj2];
Accounts *acObj3 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj3.accountNumber =@"003";
[custObj1 addCust_relationObject:acObj3];
Accounts *acObj4 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj4.accountNumber =@"004";
[custObj1 addCust_relationObject:acObj4];
//2
Customer *custObj2 = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:context];
custObj2.name= @"Bharath";
// add accoutns for customer
Accounts *acObj01 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj01.accountNumber = @"010";
[custObj2 addCust_relationObject:acObj01];
Accounts *acObj02 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj02.accountNumber = @"011";
[custObj2 addCust_relationObject:acObj02];
Accounts *acObj03 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj03.accountNumber =@"101";
[custObj2 addCust_relationObject:acObj03];
//3
Customer *custObj3 = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:context];
custObj3.name = @"Ansal";
// add accoutns for customer
Accounts *acObj001 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj001.accountNumber = @"001";
[custObj3 addCust_relationObject:acObj001];
Accounts *acObj002 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj002.accountNumber = @"002";
[custObj3 addCust_relationObject:acObj002];
Accounts *acObj003 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj003.accountNumber =@"003";
[custObj3 addCust_relationObject:acObj003];
Accounts *acObj004 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj004.accountNumber =@"004";
[custObj3 addCust_relationObject:acObj004];
//4
Customer *custObj4 = [NSEntityDescription insertNewObjectForEntityForName:@"Customer" inManagedObjectContext:context];
custObj4.name = @"Vijeesh";
// add accoutrns for customer
Accounts *acObj0001 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj0001.accountNumber = @"010";
[custObj4 addCust_relationObject:acObj0001];
Accounts *acObj0002 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj0002.accountNumber = @"011";
[custObj4 addCust_relationObject:acObj0002];
Accounts *acObj0003 = [NSEntityDescription insertNewObjectForEntityForName:@"Accounts" inManagedObjectContext:context];
acObj0003.accountNumber =@"101";
[custObj4 addCust_relationObject:acObj0003];
[context save:nil];
}
转发这些已保存的数据并过滤或使用谓词获取
-(void)fetchCoreDAta
{
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Customer" inManagedObjectContext:self.managedObjectContext];
NSEntityDescription *entityAccounts = [NSEntityDescription entityForName:@"Accounts" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest1 setEntity:entityAccounts];
// [fetchRequest1 setEntity:entity];
#pragma mark - using predicates for fetching customer name who having three accounts
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"cust_relation.@count == 4"];
// NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"ANY account_relation.name LIKE[c] 'Vijeesh'"];
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"ANY account_relation.name like 'Vijeesh'"];
NSString *value1 =@"Ansal";
NSString *value2 =@"Vijeesh";
// NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"(name == %@) AND (SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count > 0)", @"Yeswant", value1, value2] ;
[fetchRequest setPredicate:predicate];
[fetchRequest1 setPredicate:predicate1];
NSError *error = nil;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSArray *resultAccounts = [self.managedObjectContext executeFetchRequest:fetchRequest1 error:&error];
if (error)
{
NSLog(@"Unable to execute fetch request.");
NSLog(@"%@, %@", error, error.localizedDescription);
}
else
{
NSLog(@"%@", result);
NSLog(@"accounts:%@",resultAccounts);
NSMutableArray * userNAmeArray = [result valueForKey:@"name"];
NSMutableArray * accountsnumberArray = [resultAccounts valueForKey:@"accountNumber"];
NSLog(@"names:%@",userNAmeArray);
NSLog(@"acnums:%@",accountsnumberArray);
}
}