2
votes

I am adding values into 2 entities within my core data model and the problem I am facing is trying to properly retrieve the NSSet and target the associated strings when accessing the detail view.I would simply like to display the results in a uitableview. I believe the values are being correctly added along with the relationships to the entities as I previously got help off the forum for it as viewable here. I am trying to recall the associated RoutinesDetail data on the detail view.

enter image description here

I know the seague is working correctly as I am able to set the title based on the selectedRow, so it is passing the data between view controllers. I can recall 'Routines' data using Ex.routinename

Using the following to debug and try to allocate the NSSet as I read a way to display it was to use allObjects but this didn't work:

NSArray *test= [Ex.routinedet allObjects];
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                     ascending:YES];
test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

NSLog(@"Image *** %@",test);

This produces the following report which makes me question whether I am doing it right?

"<RoutinesDetails: 0x7453d80> (entity: RoutinesDetails; id: 0x74531f0 <x-coredata://C075DDEC-169D-46EC-A4B7-972A04FCED70/RoutinesDetails/p1> ; data: <fault>)" )

So basically I am looking at how to retrieve RoutinesDetails. Any suggestions would be greatly appreciated.

**

In case it is needed here is the associated code

**

routines.h - NSManagedObject

@class RoutinesDetails;

@interface Routines : NSManagedObject

@property (nonatomic, retain) id routinename;
@property (nonatomic, retain) NSSet *routinedet;

@end

@interface Routines (CoreDataGeneratedAccessors)

- (void)addRoutinedetObject:(RoutinesDetails *)value;
- (void)removeRoutinedetObject:(RoutinesDetails *)value;
- (void)addRoutinedet:(NSSet *)values;
- (void)removeRoutinedet:(NSSet *)values;

@end

RoutinesDetails.h - NSManagedObject

@class Routines;

@interface RoutinesDetails : NSManagedObject

@property (nonatomic, retain) NSString * image;
@property (nonatomic, retain) NSString * name;

@property (nonatomic, retain) Routines *routineinfo;

@end

FBCDRoutineDetailViewController(sorry if its too much code)

#import "FBCDRoutineDetailViewController.h"
#import "FBCDRoutineViewController.h"
#import "Routines.h"
#import "RoutinesDetails.h"

@interface FBCDRoutineDetailViewController ()

@end

@implementation FBCDRoutineDetailViewController
@synthesize managedObjectContext;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize Ex;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"RoutinesDetails" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:managedObjectContext sectionNameKeyPath:nil
                                                   cacheName: nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

- (void)viewDidLoad
{
    [[self navigationController] setNavigationBarHidden:NO animated:YES];

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSArray *test= [Ex.routinedet allObjects];
    NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                      ascending:YES];
    test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

    NSLog(@"Image *** %@",test);

    self.title = Ex.routinename;

    NSLog(@"Image *** %@",Ex.routinename);

    }


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view delegate

- (IBAction)save:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)cancel:(id)sender {

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{    }

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id  sectionInfo =
    [[_fetchedResultsController sections] objectAtIndex:section];
    return [sectionInfo numberOfObjects];
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    //RoutinesDetails *info = [_fetchedResultsController objectAtIndexPath:indexPath];

    NSArray *test= [Ex.routinedet allObjects];
    NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"image"
                                                         ascending:YES];

    test = [test sortedArrayUsingDescriptors:[NSArray arrayWithObject:desc]];

    //cell.textLabel.text = info.image;

    cell.textLabel.text = [[test objectAtIndex:indexPath.row] name];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"routineCell";

    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Set up the cell...
    [self configureCell:cell atIndexPath:indexPath];

    return cell;
}

Any suggestions would be greatly appreciated.

1

1 Answers

3
votes

I think you are seeing what you want. I assume EX is an exercise, a routine, and that you are trying to print out the associated details. Your debugging statement displays one RoutinesDetails (entity: RoutinesDetails; id: 0x74531f0 ; data: )" )

Try this to help you visualize

for (RoutinesDetails *detail in Ex.routinedet) {
  NSLog(@"image: %@, name: %@, routineName: %@",detail.image, detail.name, detail.routineinfo.routinename);
}