0

I have 2 entities Locations and Items. Many to many relationship.

So each item can have multiple locations and any location can have multiple items.

I'm parsing an XML of items and than trying to add locations.

So I have ManagedObject item and I have just inserted the location, so what's the syntax (code) to set the items location?

I see only add and remove in ManagedObject Class.

Is below is valid where currentItem,location are two ManagedObjects and setItem_location_relationship is the name of relationship in Items entity

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

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"location_id = %@",
                                  [locationsArray objectAtIndex:i]];
        [fetchRequest setPredicate:predicate];

        NSError *error = nil;
        NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
        if (fetchedObjects == nil) {

        }

        [fetchRequest release];

        Locations *location = [fetchedObjects objectAtIndex:0];

        NSSet *set = [NSSet setWithObject:location];

        [currentItem setItem_location_relationship:set];
Rouslan Karimov
  • 585
  • 2
  • 7
  • 19

1 Answers1

3

You should subclass your NSManagedObject. Xcode will write the class files for you. Then everything becomes easy - Xcode will generate the methods for you. Assuming your entities are called Item and Location and the to-many relationship in Item is called locations, these definitions in Item.h should look like this:

- (void)addLocationsObject:(NSManagedObject *)value;
- (void)removeLocationsObject:(NSManagedObject *)value;
- (void)addLocations:(NSSet *)values;
- (void)removeLocations:(NSSet *)values;

So to add a location object:

Location *loc = [NSEntityDescription insertNewObjectForEntityForName:"Location"
   inManagedObjectContext:self.managedObjectContext];
// ... configure loc with info from the xml feed
// or use a fetched location object
[currentItem addLocationsObject:loc];
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • Yes I have these methods. but my question is how I actually set the existing location to the item I create. – Rouslan Karimov Oct 05 '11 at 23:12
  • What I meant to say is that I don't want to add Location , what should I do if it already exists, so I want to link the item to the existing location. Or is it exactly what it does? – Rouslan Karimov Oct 05 '11 at 23:22
  • Right, this is exactly what it does. It just establishes the link ("relationship") between the existing instances of the entities. – Mundi Oct 05 '11 at 23:27
  • Thanx for your help and answering all my questions – Rouslan Karimov Oct 05 '11 at 23:28
  • 1
    @tristan You need to set up the model and then generate your managed object class files - the methods will be generated automatically. – Mundi Feb 27 '13 at 08:15
  • My older NSManagedObject subclasses have custom methods, if I recreate those managed subclasses, how to keep custom methods?Thanks. – tristan Feb 27 '13 at 15:33
  • You should move your custom methods to separate files as categories. This way, you can easily regenerate the managed object classes! – Mundi Feb 27 '13 at 20:20