Question

I built what I thought would be a simple program to explore the issue of CoreData relationships.

However, these two methods are unexpectedly looping after a segue (and arriving successfully at the destination VC). The methods involved are numberOfRowsInComponent (loops 4 times) and titleForRow (loops 5 times), creating this NSLog output before continuing:

2014-03-01 12:12:26.231 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.233 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.236 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.237 EntitiesAndRelationships[64785:a0b] Number of categories is 2
2014-03-01 12:12:26.239 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)
2014-03-01 12:12:26.242 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>)
2014-03-01 12:12:26.246 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)
2014-03-01 12:12:26.247 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8c110> (entity: WMDGCategory; id: 0x8a8e2d0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p1> ; data: <fault>)
2014-03-01 12:12:26.249 EntitiesAndRelationships[64785:a0b] Description of row is <WMDGCategory: 0x8a8a420> (entity: WMDGCategory; id: 0x8a8e2c0 <x-coredata://75EA0FB2-AB3F-480A-9EBE-CDA14EDEB902/WMDGCategory/p2> ; data: <fault>)

Here's the code for the two methods:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES];
    NSInteger *catCount;
    catCount = frc.fetchedObjects.count;
    NSLog(@"Number of categories is %zd", catCount);
    return frc.fetchedObjects.count;
}

#pragma mark Picker View delegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
    NSString * description;
    description = [[frc.fetchedObjects objectAtIndex:row]description];
    NSLog(@"Description of row is %@", description);
    return [[frc.fetchedObjects objectAtIndex:row]description];
}

I'm pretty puzzled. Can someone help, please?

Thanks!

Edit per reecon's answer below

I moved this line:

frc = [WMDGCategory MR_fetchAllGroupedBy:nil withPredicate:nil sortedBy:@"name" ascending:YES];

into viewDidLoad, but the looping persists.

Was it helpful?

Solution

Your data source and delegate methods can get called any number of times - that is not a problem.

You should assume that methods:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

or

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

can be called at any time and several times. Thus the implementation of it should be lightweight. Move the fetching code elsewhere - e.g. to viewDidLoad. So you only fetch once. Then when the picker requests information from its delegate, it does not result in performing a not needed fetch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top