Ios 如何基于双向关系从核心数据中提取数据?
在我的示例应用程序中,我有两个实体,分别称为Accounts和Customer 对于这些实体,我创建了两个NSManagedObject子类 imgur.com/zmt1N.png 现在确切地说,我不知道它是否正确,我必须保存到这些核心数据实体并检索详细信息,如“帐户表中有三个以上帐户的客户”” 期待一点详细的解释。提前感谢使用NSPredicateIos 如何基于双向关系从核心数据中提取数据?,ios,core-data,Ios,Core Data,在我的示例应用程序中,我有两个实体,分别称为Accounts和Customer 对于这些实体,我创建了两个NSManagedObject子类 imgur.com/zmt1N.png 现在确切地说,我不知道它是否正确,我必须保存到这些核心数据实体并检索详细信息,如“帐户表中有三个以上帐户的客户”” 期待一点详细的解释。提前感谢使用NSPredicate -(void)fetchCoreDAta { NSFetchRequest *fetchRequest = [[NSFetchReq
-(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);
}
}
一个简单的示例,使用不区分大小写的字符串“搜索”名称:
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];
}
是的,我们可以用它。但是我将如何与这两个表关联(我在创建表时创建了关系,但我不知道这些实体是如何相互关联的)。比如一个人在下一个账户实体中拥有两个以上的账户。插入值时是否也要执行任何特定操作?我必须从第一个实体获取客户,该客户拥有三个或更多帐户,然后从下一个实体获取帐户