views:

745

answers:

1

With the below code I'm creating a fetch request. The problem I'm having is getting the fetch request data and doing something with it like putting it into a variable (like I'm trying to do below with the firstManagedObject variable). I.e., I don't understand what to do with my "result" NSArray that I've created (shown in below code) to get usable data that isn't in the form of "(entity: Question; id: 0x10b6250 <-coredata://90FA9FD7-4CFC-4039-8A0C-40116055CADF/Question/p2 ; data: fault)" or similar. In the NSLog that I create (on the last line) I log the "result" NSArray filled from an executeFetchRequest (shown in bold below) but I don't know how to take the next step to get the actual "Question" text. Any help is appreciated. Thanks.

NSLog results:

fetch request getQuestionsByParent: 2 found (sub variables:{ formId = "9822217D-6A55-4475-88EC-E2552B336E1B"; sectionNumber = 2; }, results:( (entity: Question; id: 0x10a8720 x-coredata://90FA9FD7-4CFC-4039-8A0C-40116055CADF/Question/p2 ; data: fault), (entity: Question; id: 0x1092920 x-coredata://90FA9FD7-4CFC-4039-8A0C-40116055CADF/Question/p4 ; data: fault) ))

Code:

    NSManagedObjectContext *moc_ = [self managedObjectContext];

    NSMutableDictionary *dictionary=[[NSMutableDictionary alloc]init];
    [dictionary setObject:section.ordinal forKey:@"sectionNumber"];
    [dictionary setObject:section.parent.id forKey:@"formId"];

    NSError *error = nil;    
    NSManagedObjectModel *model = [[moc_ persistentStoreCoordinator] managedObjectModel];
    NSFetchRequest *fetchRequest = [model fetchRequestFromTemplateWithName:@"getQuestionsByParent" substitutionVariables:dictionary];
    NSAssert(fetchRequest, @"Can't find question fetch request");

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ordinal" ascending:YES];
    NSArray *sortDescriptors = [[NSMutableArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSArray *result = [[NSArray alloc] init];
    result = [moc_ executeFetchRequest:fetchRequest error:&error];
    //return result;

    NSLog(@"fetch request getQuestionsByParent: %u found (sub variables:%@, results:%@)", [result count], dictionary, result);

    NSManagedObject *firstManagedObject = [result objectAtIndex:0];
+1  A: 

You have your fetch down, you have the data. If you have implemented custom subclasses for the object you can just query the properties, if not you can use KVC to access them against the objects. So for instance, assuming you have an attribute called "myAttribute" then:

NSManagedObject *firstManagedObject = [result objectAtIndex:0];
NSLog(@"firstObject.myAttribute: %@", [firstObject valueForKey:@"myAttribute"]);

And if you have a custom subclass for the object (Question) that declares the attributes as dynamic properties then it would be:

Question *firstQuestion = [result objectAtIndex:0];
NSLog(@"firstQuestion.myAttribute: %@", firstQuestion.myAttribute);
Louis Gerbarg