我整天都在为此苦苦挣扎。我有一个 CD 模型,它有一个实体 sales_rep,它与另一个名为 customers 的实体有一对多的关系。
我可以检索并显示存储在此处的 sales_rep 数据:
SALES_REP_DATA* thisSalesRepData
我可以计算客户数量:
thisSalesRepData.hasCustomers.count
这将返回正确数量的记录(在本例中为 37)
但我无法获取客户信息。我没有设置 CD,所以实体是 CUSTOMER,但类是 CUSTOMER_DATA。
如果我这样做:
NSArray* arrCustomers = [thisSalesRepData.hasCustomers allObjects];
for(CUSTOMER_DATA* thisCustomer in arrCustomers) {
DDLogVerbose(@"%@", thisCustomer.customer_name);
}
我得到 NULL
如果我记录这个客户,我会得到:
Thursday, 07 November 2013 | 15:14:37 | AppViewController | getClientList: | 312 | <CUSTOMER_DATA: 0x8c85260> (entity: CUSTOMER; id: 0x8c849a0 <x-coredata://AC4450A8-3D03-4E9A-8EFD-47055FA0683A/CUSTOMER/p173> ; data: <fault>)
如何获取客户信息?
编辑:这是我设置客户的方式 - 我已经验证 dictThisCustomerRevised 具有值。:
//loop
//get the entity description for this class
NSEntityDescription* customerEntityDescription = [NSEntityDescription entityForName:@"CUSTOMER" inManagedObjectContext:_moc];
//init class
CUSTOMER_DATA* thisCustomer = [[CUSTOMER_DATA alloc]initWithEntity:customerEntityDescription insertIntoManagedObjectContext:_moc];
//populate it
thisCustomer.cf1_level_1_cd = [dictThisCustomerRevised objectForKey:@"cf1_level_1_cd"];
thisCustomer.customer_address = [dictThisCustomerRevised objectForKey:@"customer_address"];
thisCustomer.customer_city = [dictThisCustomer objectForKey:@"customer_city"];
thisCustomer.customer_zip_base = [dictThisCustomer objectForKey:@"customer_zip_base"];
thisCustomer.customer_num = [dictThisCustomer objectForKey:@"customer_num"];
thisCustomer.customer_name = [dictThisCustomer objectForKey:@"customer_name"];
thisCustomer.customer_class_cd = [dictThisCustomer objectForKey:@"customer_class_cd"];
thisCustomer.state_cd = [dictThisCustomer objectForKey:@"state_cd"];
[arrSalesRepCustomers addObject:thisCustomer];
NSSet* setSalesRepCustomers = [[NSSet alloc] initWithArray:arrSalesRepCustomers];
[thisSalesRepData addHasCustomers:setSalesRepCustomers];
//save this
NSError* error;
[_moc save:&error];