Search code examples
xcodecore-dataios6

Core Data Relationship


I am using Core Data for my app, but I am stuck at the moment with this problem.

My app is working fine with Core Data model 2 Entities

UserDetails and SiteLocation

enter image description here,

So I fetch the data to the UserDetails and NSPredicate to create the SiteLocations from the Users "UserProject" add to a UITableView using the fetchController and this works fine. I get a list of all the Projects.

Now I want to add the UserDetails postcode attribute to the SiteLocation entity - for Attribute sitePostCode so I can display all the Sitelocation on the map.

How do I fetch a second attribute postcode to the SiteLocation entity?

@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;

}

Creating the UserDetails code

  +(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;

}

Solution

  • Maybe I did't understand your question very well but why don't you change the method

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

    to

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

    So, when you create a UserDetails pass also the code you are interested in:

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

    Within SiteLocation category

    request.predicate = [NSPredicate predicateWithFormat:@"siteName = %@ AND postCode = %@", sitename, code];
    
    // other code here...
    
    projectName.siteName = sitename;
    projectName.sitePostCode = code;
    

    Is this ok for you?