HurkNburkS
HurkNburkS

Reputation: 5510

fetch coredata relationship

I would like to know how to fetch the items from my coredata relation ship. I guess it should be a dictionary or arrays or something that gets returned so that you can have your one to many thing.

I am quite lost at this I know how to write/read single objects but this relationship stuff is abit confusing.

I think I have sucsessfully written a relationship to coredata however now I would like to be able to read it to see if I have it right.. I have started writing the method for this but have no idea what to actually do to get all of the information out.

this is the code i have so far.. for both read and write

- (void)writeFin:(NSArray *)recivedProjectData ItemsData:(NSArray *)itemsData  {
    // WRITE TO CORE DATA
    NSManagedObjectContext *context = [self managedObjectContext];


    for (NSDictionary *dict in recivedProjectData) {
        project = [NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:context];


        project.proNumber = [dict valueForKey:@"ProNumber"];
        project.projectDescription = [dict valueForKey:@"Description"];
//        project.items = [dict valueForKey:@""]; // this is the relationship for project

    }

    for (NSDictionary *dict in itemsData) {
        items = [NSEntityDescription insertNewObjectForEntityForName:@"Items" inManagedObjectContext:context];


        items.Number = [dict valueForKey:@"Number"];
        items.Description = [dict valueForKey:@"Description"];
        items.comment = [dict valueForKey:@"Comment"];
        items.project = project; // this is the relationship for items

        [project addItemsObject:items];
    }


    NSError *error = nil;

    if (![__managedObjectContext save:&error]) {
        NSLog(@"There was an error! %@", error);
    }
    else {
        NSLog(@"created");
    }
}


- (NSMutableArray *)readFin {
    NSManagedObjectContext *context = [self managedObjectContext];


    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context];
    [fetchRequest setEntity:entity];

    NSError *error;

    NSMutableArray *projectDictionaryArray = [[NSMutableArray alloc] init];


    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
    for (ProjectList *projectList in fetchedObjects) {

        NSMutableDictionary *tempProjectDictionaryArray = [[ NSMutableDictionary alloc] init];

        [tempProjectDictionaryArray setObject:project.proNumber forKey:@"ProNumber"];
        [tempProjectDictionaryArray setObject:project.description forKey:@"Description"];
        [tempProjectDictionaryArray setObject:project.projectID forKey:@"ProjectID"];

        [projectDictionaryArray addObject:tempProjectDictionaryArray];
    }
    return projectDictionaryArray;
}

So just o reiterate, I would like to know A, is my write method look okay? B, how do you fetch/read the relationship objects from core data.

any help would be greatly appreciated.

Upvotes: 0

Views: 997

Answers (1)

Ben Pious
Ben Pious

Reputation: 4805

A relationship in Core Data isn't an object, its a property which happens to correspond to another object in your model rather than just being a dead end. You're already most of the way there as far as checking whether your relationships are ok as far as I can see -- what you need to do is add one more line in your projectList

[tempProjectDictionaryArray setObject: project.items forKey:@"items"];

the object that you will have added will be an NSSet. You can then check that things are as they should be with a loop like this after you've finished setting things up

NSSet itemsForProject = projectDictionaryArray[someIndex][@"items"]
for (Item* currItem in [itemsForProject allObjects]) {

     //access some property of the current item to make sure you have the right ones -- \
     description for example
     NSLog(@"%@", item.description);
}

Upvotes: 1

Related Questions