nsfetchedresultscontroller - Core Data Predicate one-to-many and many-to-one problem -


i have client entities , job entities.

each job can have 1 client. relationship jobs of client (client<-->>job) called jobofclient.

each client can have many jobs. relationship client of job (job<<-->client) called clientofjob.

(of course, these inverse relationships.)

i have predicates working, last 1 not. leaving out of fetchedresultscontroller set up, here of key lines of code 3 cases:

here, sort through jobs, looking jobs aren't related client:

nsentitydescription * entity = [nsentitydescription entityforname:@"job" inmanagedobjectcontext:datainterface.managedobjectcontext];  nspredicate *predicate = [nspredicate predicatewithformat: @"clientofjob == nil"]; 

here sort through jobs, looking jobs of particular client:

nsentitydescription * entity = [nsentitydescription entityforname:@"job" inmanagedobjectcontext:datainterface.managedobjectcontext]; nspredicate *predicate = [nspredicate predicatewithformat: @"clientofjob == %@", userstate.selectedclient]; 

but next 1 doesn't work. here sort through clients, looking 1 client associated selected job (or return no result if there no related client, that's not case here).

nsentitydescription * entity = [nsentitydescription entityforname:@"client" inmanagedobjectcontext:datainterface.managedobjectcontext];     nspredicate *predicate = [nspredicate predicatewithformat: @"jobofclient == %@", userstate.selectedjob]; 

the error message terminating app due uncaught exception 'nsinvalidargumentexception', reason: 'to-many key not allowed here'

there must subtle here don't understand. can me **the info have given?

jobofclient return collection of objects, means predicate doing:

nsset *jobs = [thisobject jobofclient]; return ([jobs isequal:ajob]); 

obviously, collection never equal single value, , coredata not recognize predicate. around this, think can do:

[nspredicate predicatewithformat:@"jobofclient contains %@", [userstate selectedjob]]; 

and make sure don't run again, recommend changing name of relationship jobofclient jobs (using plural form indicate it's to-many relationship, , eliminating ofclient, because it's on client entity). other relationships should renamed. clientofjob => client, etc.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -