Changing a ManagedObject's relationship?

Discussion in 'Mac Programming' started by Littleodie914, Aug 2, 2007.

  1. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #1
    Hi guys, I seem to be having relationship issues... :rolleyes:

    I've got a Subject entity, which holds lots of Task entities. I want to change one Task entity's "parentSubject" relationship to something else.

    So, lets say I've got two subjects, with "title" keys "Bio" and "Chem."

    I have the task object, and I have the ability to change the relationship using:

    Code:
    [myObject setValue:subjectObject forKey:@"parentSubject"];
    Problem is, I can't figure out how to obtain the "subjectObject." I have the string of its "title" key, and I have the list of Subject MO's using:

    Code:
    [subjectController arrangedObjects]
    But, how do I search that returned NSArray for a certain title string? I've been running through loops of "valueForKey:" and NSEntityDescription's, and I can't make heads or tails of the hierarchy. I'm sure there's an easy, simple way to do this, but I've yet to stumble upon it. :(
     
  2. thread starter macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #2
    Ah, I seem to have found my own answer. :)

    Using NSFetchRequest:

    Code:
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Subject" inManagedObjectContext:context];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entityDescription];
    	
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title ==  %@", @"Bio"]];
    [request setPredicate:predicate];
    NSError *error = nil;
    NSArray *array = [context executeFetchRequest:request error:&error];
    	
    NSManagedObject *subjectObject = [array objectAtIndex:0];
    The above will return the ManagedObject with the title "Bio." Hope this helps anyone with a similar issue. ;)
     

Share This Page