Note that there are some explanatory texts on larger screens.

plurals
  1. POcoredata relationship is set nil automatically
    text
    copied!<p>I have two entities. (Deal, Customer) Deal and Customer have 1:1 relationship. so Deal has customer, and Customer has deal.</p> <p>first, I made Customer object named "John". second, I made Deal object and set customer with "John" (#1 deal) third, I made another Deal object and set customer with "John" (#2 deal)</p> <p>at that time, I found some problem. that is #1 deal's customer set nil automatically, and #2 deal's customer is "John".</p> <p>how can I solve that?</p> <p>ps1. I got the data from web server as JSON like this deals = [id: .., ..., customer: { ... }]</p> <p>ps2. I update objects whenever receive data from server.</p> <pre><code>+ (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 the fetch NSError *error = nil; NSArray *matches = [context executeFetchRequest:request error:&amp;error]; // Check what happened in the fetch if (!matches || ([matches count] &gt; 1)) { // nil means fetch failed; more than one 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload