ios - Query table based on a value in FK -


i have couple of tables:

user:   - userid (nstring)   - name (nsstring)  report:    - reportname (nsstring)   - user (foreign key user table) 

i have userid , want reports user. have query user first core data?

if bad trying implement find , update logic outline apple. in case have 1 report per user. getting array of reports server , each report contains userid (assigned server). if don't have report user yet need create one. if have report user need update new report.

so create array of nsstring (userid's) use in predicate:

nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] init]; [fetchrequest setentity:[nsentitydescription entityforname:@"report" inmanagedobjectcontext:context]]; [fetchrequest setpredicate: [nspredicate predicatewithformat:@"(userid in %@)", userids]]; 

however in case userid not exist, user foreign relationship.

i not make foreign relationship , store userid in report need manually join later. better option.

the notion of foreign key not exist in core data. sure, they're used 1 of possible backing stores, foreign key hidden. in place, have instead relationship.

if have relationship established, walk keypath. no further fetch necessary.

you need inverses relationships. if report has user, user has reports.

for purposes, suggest implementing reportwithreportname: (does fetch), findorcreatewithreportname: (tries fetch, insert if necessary), userwithuserid: (does fetch), , findorcreateuserwithuserid:name: (fetch, insert if needed).

report *areport; user *someuser;  nslog(@"%@", areport.user.name, areport.user.userid); nslog (@"%@", someuser.userid, someuser.reports); foreach (report *report in someuser.reports) {     nslog(@"%@", report.reportname); } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -