ios - coredata relationship is set nil automatically -
i have 2 entities. (deal, customer) deal , customer have 1:1 relationship. deal has customer, , customer has deal.
first, made customer object named "john". second, made deal object , set customer "john" (#1 deal) third, made deal object , set customer "john" (#2 deal)
at time, found problem. #1 deal's customer set nil automatically, , #2 deal's customer "john".
how can solve that?
ps1. got data web server json deals = [id: .., ..., customer: { ... }]
ps2. update objects whenever receive data server.
+ (deal *)dealwithdealsdictionary:(nsdictionary *)dic inmanagedobjectcontext:(nsmanagedobjectcontext *)context { deal *deal = nil; nsfetchrequest *request = [nsfetchrequest fetchrequestwithentityname:@"deal"]; request.predicate = [nspredicate predicatewithformat:@"deal_id = %@", [dic[@"id"] description]]; // execute fetch nserror *error = nil; nsarray *matches = [context executefetchrequest:request error:&error]; // check happened in fetch if (!matches || ([matches count] > 1)) { // nil means fetch failed; more 1 impossible (unique!) deal = [matches lastobject]; // handle error } else if (![matches count]) { deal = [nsentitydescription insertnewobjectforentityforname:@"deal" inmanagedobjectcontext:context]; } else { deal = [matches lastobject]; } deal.deal_id = [dic[@"id"] description]; deal.deal_status = [dic[@"deal_status"] description]; deal.deal_stage = [dic[@"deal_stage"] description]; deal.deal_desc = [dic[@"deal_desc"] description]; deal.localized_deal_status = [dic[@"localized_deal_status"] description]; deal.localized_deal_stage = [dic[@"localized_deal_stage"] description]; if (dic[@"customer"]) { [context performblock:^{ deal.customer = [customer customerwithdictionary:dic[@"customer"] inmanagedobjectcontext:context]; }]; } return deal; }
you don't have 1:1 relationship:it 1:n
2 deals have same customer, 1 customer has n deals.
coredata wanted keep 1:1 constraints 1 deal has 1 unique customer , vice versa.
change one-to-many
Comments
Post a Comment