2

I am trying to populate a UIPickerView with results from a fetch. I was able to do it. Since there are duplicates, I decided to use the following code to get distinct records and only the required properties. The code works fine in that the array array1 & dictlevel1 have correct data (shown below). I cannot figure out how to get that array1 into a picker view?

    NSError *error;
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Factors" inManagedObjectContext:managedObjectContext];

    [fetchRequest setEntity:entity];
    [fetchRequest setResultType:NSDictionaryResultType];
    [fetchRequest setReturnsDistinctResults:YES];
    [fetchRequest setPropertiesToFetch:@[@"level1"]];

    self.title = @"Factors";
    array1 = [[NSArray alloc] init];
    array1 = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
    NSDictionary *dictlevel1 = [array1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]];

    NSLog(@"%@", array1[1]);
    NSLog(@"%@", [dictlevel1 dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"level1"]]);

Here is the output of NSLog:

2012-10-23 14:55:47.774 CoreData2[1477:c07] (
        {
        level1 = "External Combustion Boilers";
    },
        {
        level1 = "Internal Combustion Engines";
    },
        {
        level1 = "Industrial Processes";
    },
        {
        level1 = "Petroleum and Solvent Evaporation";
    },
        {
        level1 = "Waste Disposal";
    },
        {
        level1 = "Stationary Source Fuel Combustion";
    },
        {
        level1 = "Miscellaneous Area Sources";
    },
        {
        level1 = "Mobile Sources";
    },
        {
        level1 = "Solvent Utilization";
    },
        {
        level1 = "Storage and Transport";
    },
        {
        level1 = "Waste Disposal, Treatment, and Recovery";
    },
        {
        level1 = "Natural Sources";
    }
)

2012-10-23 14:55:47.775 CoreData2[1477:c07] {
    level1 =     (
        "External Combustion Boilers",
        "Internal Combustion Engines",
        "Industrial Processes",
        "Petroleum and Solvent Evaporation",
        "Waste Disposal",
        "Stationary Source Fuel Combustion",
        "Miscellaneous Area Sources",
        "Mobile Sources",
        "Solvent Utilization",
        "Storage and Transport",
        "Waste Disposal, Treatment, and Recovery",
        "Natural Sources"
    );
}
2
  • Quick code comment: You don't need this initialization line: array1 = [[NSArray alloc] init];. Your will be returned an NSArray instance from executeFetchRequest:error:. If you're not using ARC, you're also leaking that initialized array. Oct 23, 2012 at 20:40
  • Thanks for the tip. I will change that in my code.
    – Sriki
    Oct 24, 2012 at 14:34

2 Answers 2

0

I would recommend using a code snippet like this to get distinct results (@distinctUnionOfObjects) for a given key (e.g. endCalYear) in your result dictionary:

NSArray *distinctYears = [periodsFilteredByDates
                valueForKeyPath:@"@distinctUnionOfObjects.endCalYear"];

In pickerView:numberOfRowsInComponent: you can then return the number of rows with distinctYears.count and in pickerView:titleForRow:forComponent: you could use a code snippet like this to return a string for each title:

NSNumber *endCalYear = distinctResults[row];
NSString *rowTitle = [NSString stringWithFormat:@"%@",endCalYear];
return rowTitle;
0

Much like a UITableView, you must handle the UIPickerViewDataSource and UIPickerViewDelegate protocol methods, which provide the picker with it's data. See the linked documentation for specifics.

1
  • I used that. The UIPickerView works fine with the data loaded normally. My trouble arises because there are duplicates in the data store and so I want to fetch only unique values. So, I used 'NSDictionaryResultType' and fetch only the columns that I needed. The results that are returned are different (Dictionary type?) and I cannot use that in the UIPickerView methods.
    – Sriki
    Oct 24, 2012 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.