如何在一对多的核心数据关系中获取特定数据? [英] How to fetch Specific Data in One to Many Core Data Relationships?

查看:137
本文介绍了如何在一对多的核心数据关系中获取特定数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经对核心数据关系做了一个示例演示。我有一个表用户连接另一个表帐户在一对多关系的形式。



代码



   - (IBAction)savePersonData:(id)sender 
{

NSManagedObjectContext * context = appDelegate.managedObjectContext;

NSManagedObject * newDevice = [NSEntityDescription insertNewObjectForEntityForName:@PersoninManagedObjectContext:context];
[newDevice setValue:self.personName.text forKey:@name];
[newDevice setValue:self.personAddress.text forKey:@address];
[newDevice setValue:self.personMobileNo.text forKey:@mobile_no];


NSManagedObject * newAccount = [NSEntityDescription insertNewObjectForEntityForName:@AccountinManagedObjectContext:context];
[newAccount setValue:self.accountNo.text forKey:@acc_no];
[newAccount setValue:self.accountType.text forKey:@acc_type];
[newAccount setValue:self.balance.text forKey:@balance];

NSLog(@Saved Successfully);
[self dismissViewControllerAnimated:YES completion:nil];

}



图片是



我的问题是



我找到这么多时间,但找不到正确的答案。我第二次发布此问题。



我的问题是我已经插入Manually三人与他们的帐户的详细信息。



现在,我想要一个我已经输入特定



Ex



1)输入第一个移动号码。应显示第一人的余额。
2)输入第二个移动号码。应显示第二人称余额。
1)输入第3个移动数字。



余额检查代码



  (IBAction)checkBalance:(id)sender 

{NSFetchRequest * fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@Person];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@mobile_no =%@,self.textField.text];

[fetchRequest setPredicate:predicate];
NSError * error;
NSArray * result = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:& error];
if(!([result count] == 0))
{

NSManagedObjectContext * managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest * newFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@Account];
NSMutableArray * temp = [[managedObjectContext executeFetchRequest:newFetchRequest error:nil] mutableCopy];

for(NSManagedObject * temp in temp)
{
NSString * intValue = [object valueForKey:@balance];
NSString * alertString = [NSString stringWithFormat:@%@,intValue];
[self displayAlertView:@可用余额withMessage:alertString];
}
}
else
{
[self displayAlertView:@ErrorwithMessage:@请输入您在银行输入的有效手机号码]
}
}



图片





输出






我想要与我输入的特定人员平衡Person移动号码不在TextField上。



对不起,我已经问了第二次,但是无法解决这个核心数据关系。



谢谢。

解决方案

第一... b
$ b

修复关系的命名。正如其他人在对你的另一个问题的评论中指出的,你已经将它们命名回到前面:在 Person 实体中,到帐户应命名为accounts而不是person。同样,在 Account 实体中,与 Person 的一对一关系应命名为person而不是accounts。



下一步作为一对多,此问题中的 savePersonData 代码只创建一个帐户和一个 - 但不会设置它们之间的关系。 (您可以在您的输出中看到: Account 对于其accounts关系为nil。)



你的上一个问题的代码确实设置了关系(通过添加 newAccount newPerson 对象上的关系)。在您的代码中,您可以使用(修正关系名称之后):

  NSMutableSet * setContainer = [newDevice mutableSetValueForKey:@accounts ]; 
[setContainer addObject:newAccount];

但是对于一对多关系,更容易设置逆关系:

  [newAccount setValue:newDevice forKey:@person]; 

下一步...



您的 checkBalance 方法正确获取了mobile_no属性匹配的任何 Person 对象。但你的后续代码然后提取ALL Account objects - 即使你已经正确设置了关系。



只有与给定 Person 相关的帐户对象,这正是accounts关系所代表的。因此,您可以使用该关系访问相关的帐户对象:

  if(!([result count] == 0)){
NSManagedObject * requiredPerson =(NSManagedObject *)result [0];
NSSet * temp = [requiredPerson valueForKey:@accounts];
for(NSManagedObject * temp in temp){
NSString * intValue = [object valueForKey:@balance];
NSString * alertString = [NSString stringWithFormat:@%@,intValue];
[self displayAlertView:@可用余额withMessage:alertString];
}
}

或者,获取相关的通过指定一个遍历关系的谓词,直接访问对象(无需先获取 Person 对象):

  NSManagedObjectContext * managedObjectContext = appDelegate.managedObjectContext; 
NSFetchRequest * newFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@Account];
NSPredicate * predicate = [NSPredicate predicateWithFormat:@people.mobile_no =%@,self.textField.text];
[newFetchRequest setPredicate:predicate];

NSMutableArray * temp = [[managedObjectContext executeFetchRequest:newFetchRequest error:nil] mutableCopy];

for(NSManagedObject * temp in temp)
{
NSString * intValue = [object valueForKey:@balance];
NSString * alertString = [NSString stringWithFormat:@%@,intValue];
[self displayAlertView:@可用余额withMessage:alertString];
}


I have made one sample demo on core data relationships.I have one Table "User" which is connected another table "Account" In form of "One to Many" relation.

Code

-(IBAction)savePersonData:(id)sender
{

    NSManagedObjectContext *context = appDelegate.managedObjectContext;

    NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context];
    [newDevice setValue:self.personName.text forKey:@"name"];
    [newDevice setValue:self.personAddress.text forKey:@"address"];
    [newDevice setValue:self.personMobileNo.text forKey:@"mobile_no"];


    NSManagedObject *newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:context];
    [newAccount setValue:self.accountNo.text forKey:@"acc_no"];
    [newAccount setValue:self.accountType.text forKey:@"acc_type"];
    [newAccount setValue:self.balance.text forKey:@"balance"];

    NSLog(@"Saved Successfully");
    [self dismissViewControllerAnimated:YES completion:nil];

}

Image is

My Question is

I have find so many time but could not find proper answer.So I post this question second time.

My question is I have insert Manually Three person With their Account's Details.

Now ,I Want A balance which I have entered for specific person when I enter Mobile number.

Ex

1)Enter 1st Mobile Num. Should be display first Person's Balance. 2)Enter 2nd Mobile Num. Should be display second Person's Balance. 1)Enter 3rd Mobile Num. Should be display third Person's Balance.

Balance Check Code

-(IBAction)checkBalance:(id)sender

{ NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"mobile_no = %@",self.textField.text];

    [fetchRequest setPredicate:predicate];
    NSError *error;
    NSArray *result = [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if(!([result count] == 0))
    {        

        NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
        NSFetchRequest *newFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Account"];
        NSMutableArray *temp = [[managedObjectContext executeFetchRequest:newFetchRequest error:nil] mutableCopy];

        for (NSManagedObject *object in temp)
        {
            NSString *intValue = [object valueForKey:@"balance"];
            NSString *alertString = [NSString stringWithFormat:@"%@",intValue];
            [self displayAlertView:@"Available Balance" withMessage:alertString];
        }
    }
    else
    {
        [self displayAlertView:@"Error" withMessage:@"Please Enter Valid Mobile Number That you have entered in Bank"];
    }
}

Images

Output

I want Balance With Specific Person Which I enter Person Mobile No on TextField.

Sorry Guys,I have asked second time ,But Could no able to solve this core data relationships.

Thank you.

解决方案

First...

Fix the naming of your relationships. As others have pointed out in comments on your other question, you have named them back to front: in the Person entity, the to-many relationship to Account should be named "accounts" not "person". Likewise in the Account entity, the to-one relationship to Person should be named "person" not "accounts".

Next...

Although you have defined the "accounts" relationship as to-many, the savePersonData code in this question creates only one Account and one Person - but does not then set the relationship between them. (You can see this in your Output: each Account has nil for its "accounts" relationship).

The code in your previous question did set the relationship (by adding the newAccount to the relationship on the newPerson object). In your code above you could use (after fixing the relationship names):

NSMutableSet *setContainer = [newDevice mutableSetValueForKey:@"accounts"];
[setContainer addObject:newAccount];

but with one-many relationships it is easier to set the inverse relationship:

[newAccount setValue:newDevice forKey:@"person"];

Next...

Your checkBalance method correctly fetches any Person objects whose "mobile_no" attribute matches. But your subsequent code then fetches ALL Account objects - even if you had correctly set the relationship.

If you want only those Account objects that are related to a given Person, that is precisely what the "accounts" relationship represents. So you could just use that relationship to access the related Account objects:

if(!([result count] == 0)) {
    NSManagedObject *requiredPerson = (NSManagedObject *)result[0];
    NSSet *temp = [requiredPerson valueForKey:@"accounts"];
    for (NSManagedObject *object in temp) {
        NSString *intValue = [object valueForKey:@"balance"];
        NSString *alertString = [NSString stringWithFormat:@"%@",intValue];
        [self displayAlertView:@"Available Balance" withMessage:alertString];
    }
}

Alternatively, fetch the relevant Account objects directly (without fetching the Person object first) by specifying a predicate that traverses the relationship:

NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;
NSFetchRequest *newFetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Account"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"person.mobile_no = %@",self.textField.text];
[newFetchRequest setPredicate:predicate];

NSMutableArray *temp = [[managedObjectContext executeFetchRequest:newFetchRequest error:nil] mutableCopy];

    for (NSManagedObject *object in temp)
    {
        NSString *intValue = [object valueForKey:@"balance"];
        NSString *alertString = [NSString stringWithFormat:@"%@",intValue];
        [self displayAlertView:@"Available Balance" withMessage:alertString];
    }

这篇关于如何在一对多的核心数据关系中获取特定数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆