Predicate to find core data parent object based on relationship
up vote
0
down vote
favorite
I am trying to to use predicate to find a parent object (bank) based on the relationship with the child (transaction). I have used similar code in reverse to find transactions of a bank with no problems but never tried it in reverse before. The transaction is passed from a previous vc and displays correctly. Likewise, my [_banksListData count]is correct showing several banks, but after predicate shows 0.
Any suggestions or code in Obj c or Swift greatly appreciated
Here is my code -
//search banks for relationship with transaction
_banksListData = [CoreDataHelper getObjectsForEntity:@"Banks" withSortKey:@"name" andSortAscending:YES andContext:_managedObjectContext];
NSLog(@"banks count: %lu", (unsigned long)[_banksListData count]);
// banks count ok
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bankToTransaction == %@", _transaction];
NSLog(@"bankForTransaction count: %lu", (unsigned long)([[_banksListData filteredArrayUsingPredicate:predicate] count]));
//after predicate- count is 0!!!! Why!!!!
if([[_banksListData filteredArrayUsingPredicate:predicate] count]>0)
_bank = [[_banksListData filteredArrayUsingPredicate:predicate] objectAtIndex:0];
NSLog(@"transaction is for account: %@", _bank.name);
ios core-data predicate
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I am trying to to use predicate to find a parent object (bank) based on the relationship with the child (transaction). I have used similar code in reverse to find transactions of a bank with no problems but never tried it in reverse before. The transaction is passed from a previous vc and displays correctly. Likewise, my [_banksListData count]is correct showing several banks, but after predicate shows 0.
Any suggestions or code in Obj c or Swift greatly appreciated
Here is my code -
//search banks for relationship with transaction
_banksListData = [CoreDataHelper getObjectsForEntity:@"Banks" withSortKey:@"name" andSortAscending:YES andContext:_managedObjectContext];
NSLog(@"banks count: %lu", (unsigned long)[_banksListData count]);
// banks count ok
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bankToTransaction == %@", _transaction];
NSLog(@"bankForTransaction count: %lu", (unsigned long)([[_banksListData filteredArrayUsingPredicate:predicate] count]));
//after predicate- count is 0!!!! Why!!!!
if([[_banksListData filteredArrayUsingPredicate:predicate] count]>0)
_bank = [[_banksListData filteredArrayUsingPredicate:predicate] objectAtIndex:0];
NSLog(@"transaction is for account: %@", _bank.name);
ios core-data predicate
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
YourTransactionshould have an inverse to-one relationship to theBank, so you don't need to query. TheBankcan be obtained by simply accessing thebankproperty (or whatever you called the relationship) of theTransaction
– Paulw11
Nov 10 at 17:20
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
3
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.someTransaction.bankwill give you theBankobject directly. There is no need to query
– Paulw11
Nov 10 at 22:37
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to to use predicate to find a parent object (bank) based on the relationship with the child (transaction). I have used similar code in reverse to find transactions of a bank with no problems but never tried it in reverse before. The transaction is passed from a previous vc and displays correctly. Likewise, my [_banksListData count]is correct showing several banks, but after predicate shows 0.
Any suggestions or code in Obj c or Swift greatly appreciated
Here is my code -
//search banks for relationship with transaction
_banksListData = [CoreDataHelper getObjectsForEntity:@"Banks" withSortKey:@"name" andSortAscending:YES andContext:_managedObjectContext];
NSLog(@"banks count: %lu", (unsigned long)[_banksListData count]);
// banks count ok
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bankToTransaction == %@", _transaction];
NSLog(@"bankForTransaction count: %lu", (unsigned long)([[_banksListData filteredArrayUsingPredicate:predicate] count]));
//after predicate- count is 0!!!! Why!!!!
if([[_banksListData filteredArrayUsingPredicate:predicate] count]>0)
_bank = [[_banksListData filteredArrayUsingPredicate:predicate] objectAtIndex:0];
NSLog(@"transaction is for account: %@", _bank.name);
ios core-data predicate
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to to use predicate to find a parent object (bank) based on the relationship with the child (transaction). I have used similar code in reverse to find transactions of a bank with no problems but never tried it in reverse before. The transaction is passed from a previous vc and displays correctly. Likewise, my [_banksListData count]is correct showing several banks, but after predicate shows 0.
Any suggestions or code in Obj c or Swift greatly appreciated
Here is my code -
//search banks for relationship with transaction
_banksListData = [CoreDataHelper getObjectsForEntity:@"Banks" withSortKey:@"name" andSortAscending:YES andContext:_managedObjectContext];
NSLog(@"banks count: %lu", (unsigned long)[_banksListData count]);
// banks count ok
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"bankToTransaction == %@", _transaction];
NSLog(@"bankForTransaction count: %lu", (unsigned long)([[_banksListData filteredArrayUsingPredicate:predicate] count]));
//after predicate- count is 0!!!! Why!!!!
if([[_banksListData filteredArrayUsingPredicate:predicate] count]>0)
_bank = [[_banksListData filteredArrayUsingPredicate:predicate] objectAtIndex:0];
NSLog(@"transaction is for account: %@", _bank.name);
ios core-data predicate
ios core-data predicate
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Nov 10 at 17:23
Paulw11
65.4k107998
65.4k107998
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Nov 10 at 13:23
jack599
12
12
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
jack599 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
YourTransactionshould have an inverse to-one relationship to theBank, so you don't need to query. TheBankcan be obtained by simply accessing thebankproperty (or whatever you called the relationship) of theTransaction
– Paulw11
Nov 10 at 17:20
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
3
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.someTransaction.bankwill give you theBankobject directly. There is no need to query
– Paulw11
Nov 10 at 22:37
add a comment |
YourTransactionshould have an inverse to-one relationship to theBank, so you don't need to query. TheBankcan be obtained by simply accessing thebankproperty (or whatever you called the relationship) of theTransaction
– Paulw11
Nov 10 at 17:20
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
3
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.someTransaction.bankwill give you theBankobject directly. There is no need to query
– Paulw11
Nov 10 at 22:37
Your
Transaction should have an inverse to-one relationship to the Bank, so you don't need to query. The Bank can be obtained by simply accessing the bank property (or whatever you called the relationship) of the Transaction– Paulw11
Nov 10 at 17:20
Your
Transaction should have an inverse to-one relationship to the Bank, so you don't need to query. The Bank can be obtained by simply accessing the bank property (or whatever you called the relationship) of the Transaction– Paulw11
Nov 10 at 17:20
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
3
3
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.
someTransaction.bank will give you the Bank object directly. There is no need to query– Paulw11
Nov 10 at 22:37
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.
someTransaction.bank will give you the Bank object directly. There is no need to query– Paulw11
Nov 10 at 22:37
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
jack599 is a new contributor. Be nice, and check out our Code of Conduct.
jack599 is a new contributor. Be nice, and check out our Code of Conduct.
jack599 is a new contributor. Be nice, and check out our Code of Conduct.
jack599 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239381%2fpredicate-to-find-core-data-parent-object-based-on-relationship%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Your
Transactionshould have an inverse to-one relationship to theBank, so you don't need to query. TheBankcan be obtained by simply accessing thebankproperty (or whatever you called the relationship) of theTransaction– Paulw11
Nov 10 at 17:20
Thanks for your comment. In this case I am sorting transactions from several bank accounts into categories, and then passing one transaction to a new controller for editing. I don't have a reference to the bank account the transaction was originally derived from. My question is - how do I do the reverese query to get the bank account from the transaction?
– jack599
Nov 10 at 22:34
3
Why don't you have the reference? Doesn't your transaction entity have a reference to its bank entity? If it doesn't then you should change your model so that it does. Core Data is not a relational database, it is an object persistence system. References in Core Data are not "foreign keys" they are actual references to the referenced object.
someTransaction.bankwill give you theBankobject directly. There is no need to query– Paulw11
Nov 10 at 22:37