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);









share|improve this question









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.




















  • 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






  • 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

















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);









share|improve this question









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.




















  • 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






  • 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















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);









share|improve this question









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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








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.












  • 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






  • 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




















  • 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






  • 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


















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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






jack599 is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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





































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.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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




















































































Popular posts from this blog

The Sandy Post

Danny Elfman

Pages that link to "Head v. Amoskeag Manufacturing Co."