[英]How to Fetch Data From Core Data Based On Two way RelationShip?
在我的示例应用程序中,我有两个实体称为帐户和客户
为此,我创建了两个NSManagedObject子类
imgur.com/zmt1N.png
现在,我完全不知道它的正确性还是正确性,我必须保存到这些核心数据实体并检索诸如“ 在帐户表中拥有三个以上帐户的客户 ”之类的详细信息
期待一些详细的解释。 提前致谢
使用NSPredicate
一个简单的示例,其中“搜索”名称不区分大小写的字符串:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"customer"];
request.predicate = [NSPredicate predicateWithFormat:@"name LIKE[c] %@", @"Amazon"];
NSArray *matches = [self.managedObjectContext executeFetchRequest:request error:nil];
根据上面链接的NSPredicate类参考:
您可以为关系创建谓词,例如:
group.name,例如“ work *”
所有儿童年龄> 12岁
任何年龄> 12岁的儿童
这是我期望做的。 谢谢您的帮助
-(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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.