BluGeni
BluGeni

Reputation: 3454

Fetch transformable coredata element correctly

EDIT2: My main goal is to fill in a tableview with the info below, so for example, each cell would contain the address and first_name. Is there an easy way to fill the tableview from what I have?

Ok so I am having trouble loading data that I have saved into coredata correctly. I save a dictionary/array into a transformable coredata attribute. this is the nslog of what I save to coredata:

(
        {
        "ADD_CITY" = ABC;
        "ADD_LINE1" = "123 MAIN";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = JOHN;
        "LAST_NAME" = DOE;
    },
        {
        "ADD_CITY" = ABCD;
        "ADD_LINE1" = "1234 MAIN";
        "ADD_LINE2" = "";
        "ADD_STATE" = IA;
        "ADD_ZIP" = 50833;
        "BUSINESS_NAME" = "";
        "FIRST_NAME" = JOE;
        "LAST_NAME" = SMITH;
    }
)

That all is fine but my problem comes when I am trying to load it later, this is what I am doing:

 NSFetchRequest *fetchRequest2 = [NSFetchRequest fetchRequestWithEntityName:@"Locations"];
        NSEntityDescription *entity2 = [NSEntityDescription entityForName:@"Locations" inManagedObjectContext:self.managedObjectContext];


        fetchRequest2.resultType = NSDictionaryResultType;


        fetchRequest2.propertiesToFetch = [NSArray arrayWithObject:[[entity2 propertiesByName] objectForKey:@"location"]];

        [fetchRequest2 setPredicate:[NSPredicate predicateWithFormat:@"policy == %@", self.policy]];

        fetchRequest2.returnsDistinctResults = YES;

        self.dictionaries2 = [self.managedObjectContext executeFetchRequest:fetchRequest2 error:nil];
        NSLog (@"locations2: %@",self.dictionaries2);

And I get a "nested" dictionary I think?

the nslog from above:

locations2: (
        {
        location =         (
                         {
    "ADD_CITY" = ABC;
    "ADD_LINE1" = "123 MAIN";
    "ADD_LINE2" = "";
    "ADD_STATE" = IA;
    "ADD_ZIP" = 50833;
    "BUSINESS_NAME" = "";
    "FIRST_NAME" = JOHN;
    "LAST_NAME" = DOE;
},
    {
    "ADD_CITY" = ABCD;
    "ADD_LINE1" = "1234 MAIN";
    "ADD_LINE2" = "";
    "ADD_STATE" = IA;
    "ADD_ZIP" = 50833;
    "BUSINESS_NAME" = "";
    "FIRST_NAME" = JOE;
    "LAST_NAME" = SMITH;
}
        );
    }
)

How do I just get the "location" not the extra level im getting?

EDIT:

Maybe an easier way to do it would be just to remove the outer layer of the fetched result? if so how would I do that?

Upvotes: 0

Views: 382

Answers (1)

Martin R
Martin R

Reputation: 540065

A fetch request with NSDictionaryResultType returns an array of dictionaries (with a key/value pair for each fetched property). If you are only interested in the value of a particular property (in your case "location") you can use valueForKey:

NSArray *locations = [self.dictionaries2 valueForKey:@"location"];

However, from the output it looks as if you have created only a single Core Data object containing the entire array of dictionaries. Usually, one would create a Core Data object for each location. To display the data in a table view, you can use a NSFetchedResultsController.

Upvotes: 4

Related Questions