ios - Core Data Duplicate Entity -


i`m new core data , face problems many-to-many relationships,

here structure

diagrams

this structure allow 1 profile can have many itens , 1 item can in many profiles it`s store

both relations inverse

so when i`m add new item in profile, new item duplicate in table item.

itens list ==========  item9 item50 ========== 

after add new item in profile

itens list ========== item9 item50 item9 ========== 

when list profile "itens" correct 1 new item.

profile  ==========  item9  ========== 

my code operations is.

mocmanager* moc = [mocmanager sharedmocmanager]; nsarray* products = [moc listdata:@"products"]; nsmutablestring* desc = [nsmutablestring string];  if( products != nil ) {     item* itemtobuy = products[0]; // test. first     item* newitem = [item inititem];      nsentitydescription *entity = [itemtobuy entity];     (nsstring *attributename in [entity attributesbyname]) {         [newitem setvalue:[itemtobuy valueforkey:attributename] forkey:attributename];     }     (nsstring *relationshipname in [entity relationshipsbyname]) {         [newitem setvalue:[itemtobuy valueforkey:relationshipname] forkey:relationshipname];     }       [desc appendstring:newitem.identifier];      [profile additensobject:newitem];      [moc savedata]; } 

my question is.

  • can prevent duplication ?

  • if not possible prevent this. how can list itens dosen`t have profile associated ?

tks

brunno

here's way check duplicates:

entity* obj = nil; nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"entity"]; request.predicate = [nspredicate predicatewithformat:@"entity.id = %@", new_element_id];  nserror *error = nil; nsarray *matches = [context executefetchrequest:request error:&error];  if (!matches || ([matches count] > 1)) {     //nslog(@"error, have more 1 object same id"); } else if ([matches count] == 0) {     //nslog(@"new, obj not found id");     obj = [nsentitydescription insertnewobjectforentityforname:@"entity" inmanagedobjectcontext:your_context]; } else {     obj = [matches lastobject]; //only 1 obj found... update 1 }  return obj; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -