核心数据关系

时间:2012-10-03 10:33:45

标签: xcode core-data ios6

我正在为我的应用程序使用Core Data,但此刻我遇到了这个问题。

我的应用程序与Core Data model 2 Entities

一起正常运行

UserDetailsSiteLocation

enter image description here

所以我将数据提取到UserDetailsNSPredicate以使用SiteLocations从用户“UserProject”创建UITableView添加到fetchController这很好用。我得到了所有项目的清单。

现在我想将UserDetails postcode属性添加到SiteLocation实体 - 属性sitePostCode,这样我就可以在地图上显示所有Sitelocation

如何向postcode实体提取第二个属性SiteLocation

@implementation SiteLocation (Create)

 +(SiteLocation *)siteLocationWithName:(NSString *)sitename inManagedObjectContext:(NSManagedObjectContext *)context
 {
 SiteLocation *projectName = nil;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SiteLocation"];
request.predicate = [NSPredicate predicateWithFormat:@"siteName = %@",sitename];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"siteName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSError *error = nil;
NSArray *siteLocations = [context executeFetchRequest:request error:&error];

if (!siteLocations || ([siteLocations count] > 1)) {
    // handle error
    NSLog(@"site = 0");

} else if (![siteLocations count]) {
    projectName = [NSEntityDescription insertNewObjectForEntityForName:@"SiteLocation"
                                                 inManagedObjectContext:context];
    projectName.siteName = sitename;
    //projectName.sitePostCode = sitePostCode;

} else {
    projectName = [siteLocations lastObject];
}

NSLog(@"project name = %@", projectName);
return projectName;

}

创建UserDetails代码

  +(UserDetails*)userWithJsonInfo:(NSDictionary*)jsonInfo inManagedObjectContext:(NSManagedObjectContext *)context;
 {
 UserDetails *userdetail = nil;

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"UserDetails"];
request.predicate = [NSPredicate predicateWithFormat:@"userName = %@", [jsonInfo objectForKey:Employee_Name]]; // update Employee_Number
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"userName" ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];

NSError *error = nil;

NSArray *matches = [context executeFetchRequest:request error:&error];

if (!matches || ([matches count] > 1)) {
    //error handler
} else if ([matches count] == 0){
    userdetail = [NSEntityDescription insertNewObjectForEntityForName:@"UserDetails" inManagedObjectContext:context];
    userdetail.userID = [jsonInfo objectForKey:ID];
    userdetail.userName = [jsonInfo objectForKey:Employee_Name];
    userdetail.userEmail = [jsonInfo objectForKey:Email_Address];
    userdetail.userMobile = [jsonInfo objectForKey:Phone_Handheld_No];
    userdetail.userPhoto = [jsonInfo objectForKey:PhotoURL];
    userdetail.postcode = [jsonInfo objectForKey:Project_Postcode];
    userdetail.userProject = [jsonInfo objectForKey:Project_Description];
    userdetail.projectUsers = [SiteLocation siteLocationWithName:[jsonInfo     objectForKey:Project_Description]inManagedObjectContext:context];

    //NSLog(@"user detail = %@", userdetail.userName);


} else {
    userdetail = [matches lastObject];
}

return userdetail;

}

1 个答案:

答案 0 :(得分:2)

也许我不太了解你的问题,但为什么不改变方法

+(SiteLocation *)siteLocationWithName:(NSString *)sitename inManagedObjectContext:(NSManagedObjectContext *)context

+(SiteLocation *)siteLocationWithName:(NSString *)sitename postCode:(NSString*)code inManagedObjectContext:(NSManagedObjectContext *)context

因此,当您创建UserDetails传递时,您还感兴趣的代码:

userdetail.projectUsers = [SiteLocation siteLocationWithName:[jsonInfo objectForKey:Project_Description] postCode:[jsonInfo objectForKey:Project_Postcode] inManagedObjectContext:context];

SiteLocation类别

request.predicate = [NSPredicate predicateWithFormat:@"siteName = %@ AND postCode = %@", sitename, code];

// other code here...

projectName.siteName = sitename;
projectName.sitePostCode = code;

这对你好吗?