objective c - Make a relationship with one object already exists -


the user must create family in allfamille. in second view user must create product in allproduit. while creating, user must choose family created before.

allfamille , allproduit 2 different entities. how create relationship between them?

family creation:

-(ibaction)save:(id)sender {     app=[[uiapplication sharedapplication]delegate];     nsmanagedobjectcontext *context = [app managedobjectcontext];     allfamille *famille = [nsentitydescription insertnewobjectforentityforname:@"allfamille"inmanagedobjectcontext:context];             famille.name = namefamfield.text;         nserror *error;             if (![context save:&error]) {         nslog(@"erroor");     }      [[nsnotificationcenter defaultcenter] postnotificationname:@"famcreated" object:self]; } 

product creation:

-(void)addprod:(nsstring *)idprod     {             nsmanagedobjectcontext *context = [app managedobjectcontext];      nserror *error = nil;      allcodevente * _allcodevente = (allcodevente*) [nsentitydescription insertnewobjectforentityforname:@"allcodevente" inmanagedobjectcontext:context];      (nsmanagedobject *obj in app.cdeventearray)     {         _allcodevente.codevente = [obj valueforkey:@"cdv"];         _allcodevente.unitevente = [obj valueforkey:@"uv"];                 }      allproduit * _allproduit = (allproduit*) [nsentitydescription insertnewobjectforentityforname:@"allproduit" inmanagedobjectcontext:context];     _allproduit.libelleproduit = nomstr;     _allproduit.familleproduit=famillestr;     _allproduit.stockproduit =qtestockstr;     _allproduit.prixventeproduit=prixvente;     _allproduit.prixachatproduit = prixachat;     _allproduit.idproduit= idprod;      [_allcodevente addproduitobject:_allproduit];              if (![context save:&error]) {         nslog(@"erroor");     }             } 

i'm not pretty sure if understand question can follow these hints.

model entities in correct manner

this means need (i suppose) to-many relationships between allfamille (af brevity) , allproduit (ap).

for example, create to-many relationship called toallproduits af ap. create relationship called toallfamille (an inverse) ap af.

see prev answer: setting parent-child relationship in core data.

retrieve correct family

after created bunch of families, create products , associate them specific family. how?

for example, create nsfetchrequest retrieve specific af. need nspredicate. after that, use retrieved family , assign product. e.g.

allproduit * _allproduit = (allproduit*) [nsentitydescription insertnewobjectforentityforname:@"allproduit" inmanagedobjectcontext:context]; _allproduit.libelleproduit = nomstr; // other properties here... _allproduit.toallfamille = retrievedallfamille; // object has been retrieved correct request 

hope helps.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -