【发布时间】:2014-05-22 13:18:27
【问题描述】:
有一个实体,我从 JSON 填充数据,假设它是 Photographer 和 Photo。两者都有一些数据,我使用循环填充并管理 ObjectContex ..
这样,
NSMutableArray *ArrPhotographer= [[self.M3Arr objectForKey:@"LifeMag"]objectForKey:@"Photographer"];
for (int i = 0; i< ArrPhotographer.count; i++) {
Photographer * photographerObj = [NSEntityDescription insertNewObjectForEntityForName:@"PhotographerData"
inManagedObjectContext:[self managedObjectContext]];
NSMutableDictionary *tpDict = [cleanerListArr objectAtIndex:i];
photographerObj.cleanerName = [tpDict objectForKey:@"photographerName"];
}
现在我已经为摄影师和照片实体完成了这项工作,根据这张图片,我的杂志实体拥有该表中已经存在的数据。如图所示,我与杂志的照片和摄影师建立了一种关系。
现在的问题是,
如果表中已经存在摄影师姓名,如何将其与杂志实体连接。我需要那个特定位置的托管对象引用。
(例如,现在有三位摄影师 Ron、Harry 和 Sunny 对于 Photo Cover1 我想要 Ron 的名字。然后我在预填充时需要 Ron 的对象引用)。
如何获取这个对象的引用?
// **************** 编辑
我得到了对象存在...但是没有如何获取对象和 2)如何将其提供给上述 x 和 y?
I am using this code to saving the data in Magazine
Magazine *magObj = [NSEntityDescription insertNewObjectForEntityForName:@"Magazine"
inManagedObjectContext:[self managedObjectContext]];
magObj .issueID=[NSNumber numberWithInt:1];
magObj .photo= x;
magObj .photographer = y;
#### 编辑 2
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Cleaner"];
[request setPredicate:[NSPredicate predicateWithFormat:@"cleanerName = %@", @"Robin"]];
[request setFetchLimit:1];
NSUInteger count = [[self managedObjectContext] countForFetchRequest:request error:nil];
if (count == NSNotFound){
NSLog(@"ERROR FOund");
}
// some error occurred
else if (count == 0){
NSLog(@"no matching object");
}
// no matching object
else{
NSLog(@"Found Match");
}
【问题讨论】:
标签: ios objective-c core-data entity-relationship iphonecoredatarecipes