Ios coredata关系自动设置为零
我有两个实体。(交易、客户) 交易和客户有1:1的关系。所以交易有顾客,顾客有交易 首先,我创建了名为“John”的客户对象。 第二,我将交易对象设置为“John”(1笔交易) 第三,我做了另一个交易对象,并用“John”(2个交易)设置了客户 当时,我发现了一些问题。 即#1交易的客户自动设置为零,而#2交易的客户为“John” 我怎样才能解决这个问题 ps1。我从web服务器获取的数据是JSON,如下所示 交易=[id:…,客户:{…}] ps2。每当从服务器接收数据时,我都会更新对象Ios coredata关系自动设置为零,ios,core-data,Ios,Core Data,我有两个实体。(交易、客户) 交易和客户有1:1的关系。所以交易有顾客,顾客有交易 首先,我创建了名为“John”的客户对象。 第二,我将交易对象设置为“John”(1笔交易) 第三,我做了另一个交易对象,并用“John”(2个交易)设置了客户 当时,我发现了一些问题。 即#1交易的客户自动设置为零,而#2交易的客户为“John” 我怎样才能解决这个问题 ps1。我从web服务器获取的数据是JSON,如下所示 交易=[id:…,客户:{…}] ps2。每当从服务器接收数据时,我都会更新对象 +
+ (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:&error];
// Check what happened in the fetch
if (!matches || ([matches count] > 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;
}
如果您希望客户有多笔交易和/或多个客户各自有多笔交易(其中每个客户都可以有相同的交易),请建立1对多或多对多的关系
引用被设置为零,因为您说过一次只能有一个引用。如果您希望一个客户有多个交易和/或多个客户都有多个交易(其中每个客户都可以有相同的交易),请将关系设为1对多或多对多
引用被设置为nil,因为您说过一次只能有一个引用。您没有1:1的关系:它是1:N 2个交易有相同的客户,因此1个客户有N个交易 CoreData希望保持1:1的约束条件,即1笔交易始终有1个唯一客户,反之亦然
更改为一对多您没有1:1的关系:它是1:N 2个交易有相同的客户,因此1个客户有N个交易 CoreData希望保持1:1的约束条件,即1笔交易始终有1个唯一客户,反之亦然
更改为一对多谢谢,这是我的概念错误。我设置Customer.deal-to-Customer.deals(to-Many关系),它解决了。谢谢,这是我的概念错误。我将Customer.deal设置为Customer.deals(对许多关系),它就解决了。