如何将现有Core Data对象分配给另一个Core Data对象的属性?

时间:2012-09-27 23:35:50

标签: objective-c core-data nsmanagedobject

如果我搜索Customer对象并且发现客户存在,那么我想将该Customer对象分配给新invoice.customer对象的Invoice属性。我可以这样做吗?

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Customer" inManagedObjectContext:self.objectContext]];
request.returnsObjectsAsFaults=NO; //populate beverage object
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(id == %@)", customerID];
[request setPredicate:predicate];

NSError *error = nil;
NSArray *array = [self.objectContext executeFetchRequest:request error:&error];
if([array count] == 1) 
{
    invoice.customer = (Customer*)[array objectAtIndex:0];
}

我想避免的是必须将Customer的所有属性值重新分配给invoice.customer。这样做有什么考虑吗?

0 个答案:

没有答案