Gold challenge Major issue


#1

I have spent 3 whole days trying to crack this one but i think this particular problem is just not solvable for me… I have tried different ways… This is my last attempt-
So first of all i add a new method and a couple of new ivars to BNRItemStore…

BNRItemStore.h

@property NSMutableArray *assets;
@property NSManagedObject *currentAssetType;
-(NSMutableArray *)filteredSetOfAllItemsIn:(NSManagedObject *)assetType usingCurrentAsset:(BNRItem *)item;

BNRItemStore.m

-(NSMutableArray *)filteredSetOfAllItemsIn:(NSManagedObject *)assetType usingCurrentAsset:(BNRItem *)item{    
    if(!assets || (currentAssetType!=assetType)){
        currentAssetType= assetType;
        NSLog(@"assets array initialising");
        assets= [[NSMutableArray alloc] init];
    }
    if (item.assetType==currentAssetType) {
        NSLog(@"item- %@",item);
        [assets addObject:item];
    }
    return assets;
}

Then i send this method above as a message to BNRItemStore object in AssetTypePicker-
AssetTypePicker.h (2 new properties)

NSManagedObject *selectedAssetType;
@property NSMutableArray *assets;

AssetTypePicker.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Test2");
    if (section==0) {
        return [[[BNRItemStore sharedStore] allAssetTypes] count]; //3 Asset Types
    }
    else{
        NSLog(@"asset count %d",self.assets.count);
        return self.assets.count;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"Selecting a row in AssetTypePicker");
    UITableViewCell *cell= [tableView cellForRowAtIndexPath:indexPath];
    
    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
    
    NSArray *allAssets=[[BNRItemStore sharedStore] allAssetTypes];
    selectedAssetType= [allAssets objectAtIndex:indexPath.row]; //each assetType is of type NSManagedObject
    
    [item setAssetType:selectedAssetType];
    
    self.assets=[[BNRItemStore sharedStore] filteredSetOfAllItemsIn:selectedAssetType usingCurrentAsset:item];
    NSLog(@"assets- %@",self.assets);
    
    NSLog(@"popping off modal controller's view");
    [self.navigationController popViewControllerAnimated:YES];   
}

In the above scenario, i have added filteredSetOfAllItemsIn: usingCurrentAsset: for adding all the various assets (BNRItems) to their appropriate AssetTypes…
The code in this method only runs for only single case though wherein if i add 2 items and group them under AssetType- “Furniture” (for instance) consecutively,
the corresponding 2 assets(BNRItems) get added in one array object. Similarly for the next 2 items, if grouped under AssetType- “Jewelry”, the corresponding assets will be added to a new array object and so on…
But i want this particular code to also include the case wherein when i add the first item under AssetType-“Furniture” and second item under AssetType-“Jewelry” (thus creating 2 separate array objects with those items added individually in each one of them),the third item, if mapped again to AssetType-“Furniture”, ought to be added to the first array rather than creating a separate third array… The Array object creation should be per AssetType and not per Asset itself (BNRItem). However sadly, this isn’t the case currently (in the code above) and i haven’t been able to tune the code in the method to make it work for this particular case… Is there any way to get this working??
I can’t believe i spent 3 days and still couldn’t get the damn thing solved… Earlier i tried other things including using a dictionary and soon hit a dead end. Please do not suggest NSPredicate… Truth be told i do not want to solve this any other way using other techniques unless absolutely necessary i.e if there is no way i can solve using this technique above…i had to think real hard before i was able to come up with this… And still not solved it… Pls assist :confused: :confused: :confused: I am so frustrated with this chapter… Have spent 12 days over it still things aren’t clear… This chapter really ruined my experience… I want to move to the next chapter…