jamesC
jamesC

Reputation: 422

Core Data with multiple viewControllers

I'm having a lot of difficulty getting core data to work in my application. I'm perfectly comfortable with core data in a table view controller and even with core data in a single view application. I'm unable however to get my program to function properly in with multiple view controllers.

I've read through Zarra's core data book and bought pro core data for iOS and have gone through the projects listed but every one of them is used in a TableView controller. with the exception of the shapes application in pro core data for iOS.

Does anyone know of any examples (code or tutorials) that would demonstrate how to do a program with multiple view controllers and core data?

What I would like to do is have buttons on the first (instead of tableview cells) that will segue to the the next viewController. On the second view controller I would like that information populated with information from the set of the first entity,

so I have something like this so far where the first entity is:

House

Person

occupants <-->>household (one to many )

{
...
int i = //house selected on previous view controller;
NSManagedObject *people = [[self sortOccupants] objectAtIndex:i];
textField01.text = [NSString stringWithFormat:@"%@",[[people valueForKey:@"personName"]description]];
}

the sort occupants looks like this:

-(NSArray *)sortOccupants
{
NSSortDescriptor *sortPeopleInHouse = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortPeopleInHouse, nil];
return [[(NSSet *)[house valueForKey:@"occupants"] allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}

Any Ideas would be great but if you can point me to sample code that would show this I would be most appreciative.

Thanks,

Upvotes: 1

Views: 1514

Answers (2)

karwag
karwag

Reputation: 2641

The way I would approach this is to generate NSManagedObject subclasses for your entities (makes it much more readable and type-safe).

Then, I would create a new init method in the second view controller. initWithHouse:(House *)house or something:

@property (nonatomic, strong) House  * currentHouse;
@property (nonatomic, strong) NSArray * sortedOccupants; // Array of People objects

-(id) initWithHouse:(House *)house
{
  if (self = [super init])
  {
      // Managed Object Context available from [currentHouse managedObjectContext]
      currentHouse = house;
      sortedOccupants = [self sortOccupants:house.occupants];
  }
  return self;
}

-(NSArray *)sortOccupants: (NSSet *)occupants
{
    NSSortDescriptor *sortPeopleInHouse = [[NSSortDescriptor alloc] initWithKey:@"personName" ascending:YES];
    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortPeopleInHouse, nil];

    return [occupants allObjects] sortedArrayUsingDescriptors:sortDescriptors];
}

Hope that helps.

Upvotes: 1

David Braun
David Braun

Reputation: 820

Maybe the key is that each view controller should have

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

in its header.

Before you push a view controller you give it a managed object context. In this new view controller you can have your typical methods for searching and saving the context.

You start with a home view controller, which is really a list of people. You select a person and launch a person view controller. You should pass a managed object context and an instance of Person to this person view controller.

Upvotes: 1

Related Questions