我想知道这是如何工作的,如果我创建了一个 Car 对象,并且有一个名为 toParts 的关系(它包含这辆车的所有汽车零件),然后我创建了另一辆车,它会复制 toParts 集吗?
添加到核心数据:汽车 a = (toParts = 车轮、轮胎、座椅)
添加到核心数据:汽车 b =(toParts = 车轮、轮胎、座椅)
CarParts.h(具有名称属性)
我现在会有 Carparts = wheel , wheel, Tire, Tyre, seat, seat 如果是这样我需要复制吗?有没有办法将所有未来的汽车指向同一个车轮实例(而不是拥有 1000 个)?
提前致谢
编辑添加代码:
这是我现在用来获取结果的代码,我正在寻找重复项的 ris 并获得最终的汽车数组而不是 carParts(但我想通过 carPart 搜索)
-(NSArray*) loadCarsFromCoreDataUsingCarParts:(NSMutableArray*)inputCarParts{
NSLog(@"carParts =%@",inputCarParts);
NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
//To find the Cars we are using the carParts
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CarPart" inManagedObjectContext:[self managedObjectContext]];
//sets up fetch request details
[fetchRequest setEntity:entity];
//3 here signifies it is an ingredient search
[fetchRequest setPredicate:[self parseSearchObjectsIntoAPredicate:inputCarParts:3]];
//don’t understand these results so comment out
//[fetchRequest setReturnsDistinctResults:YES];
//[fetchRequest setResultType:NSDictionaryResultType];
//attempt to preload the nsset containing the cars (just one each time for some reason
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"ToCar", nil]];
//Perform fetch
NSError *error;
NSArray *records = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
//I have to do this to get the results finally as cars NOT individual instances of car parts
return [self convertToCars:records];
}
-(NSArray*)convertToCars:(NSArray*)records{
NSMutableArray *manipulator =[NSMutableArray arrayWithArray:records];
NSMutableArray *convertedArray =[[NSMutableArray alloc]init];
for (int i=0; i<[manipulator count]; i++) {
//regular way
CarPart *partFromManipulator=(CarPart *)[manipulator objectAtIndex:i];
[convertedArray addObjectsFromArray:[[ partFromManipulator toCar]allObjects]];
}
NSLog(@"Results from core data = %i",[convertedArray count]);
return [NSArray arrayWithArray:convertedArray];
}