Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-09-2011, 03:44 PM   #1 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default Advanced Core Data Model Help

I'm making a school planner app and I'm having a heck of a time with core data. I have an EXTREMELY advanced data model. Here is what I've got so far:



As you can see, I wasn't kidding about the level of simplicity. Let me explain it.

A course is the main relational entity here; however, none of the attributes or relationships are required. There can be multiple courses. Each course can optionally contain a name and location. Each course can also optionally contain multiple classDay and teacher entities (many-to-many relationship) and one period entity (many-to-one relationship).

And of course, all of these can be inverted: While a course can be on multiple class days, a class day can be associated with multiple courses. While a course can have multiple teachers, a teacher can teach multiple courses. And finally, While a course can be during one period, a period can be associated with one course. Also, a period can be during one PeriodTime.

A course can also have multiple assignments; however, an assignment can only be for one course. An assignment can also be of multiple types and vise versa.

When you delete a course, all of the assignments associated with it are deleted as well. When you delete a period, the period's time is deleted as well. And when you delete an assignment type, all the assignments associated with that type are deleted as well.

-------------------------------

Like I said, VERY complex. I guess my question is: how would I deal with so many different relationships like this? Would I need multiple managedObjectContexts for each different relationship? Sorry for the long question. I've been working at this for about a week and a half now and it's driving me crazy!

Last edited by Whitehk; 07-11-2011 at 07:06 PM.
Whitehk is offline   Reply With Quote
Old 07-09-2011, 04:35 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

It's not THAT complicated. They didn't write the framework just to deal with one or two entities.

And your question is too vague. What are you having trouble doing?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-09-2011, 05:01 PM   #3 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
It's not THAT complicated. They didn't write the framework just to deal with one or two entities.

And your question is too vague. What are you having trouble doing?
Well, specifically, It crashes and I get

"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'"

whenever I try to create my fetchedResultsController in the console. Like I said in the previous post, how would I go about handling these different relationships? To me this sounds like you have to make certain modifications for a to-one relationship and different modifications for a to-many relationship. Would I be correct in saying this?

Here's the code to create the controller:

Code:
- (NSFetchedResultsController *)fetchedResultsController {
    
    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Course" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"classDay.dayName" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    
    [fetchRequest setFetchBatchSize:20];
    
    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    fetchedResultsController.delegate = self;
    
    [sort release];
    [fetchRequest release];
    [theFetchedResultsController release];
    
    return fetchedResultsController;    
    
}
Here's my viewDidLoad where I get the crash:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];

    NSError *error;
	if (![[self fetchedResultsController] performFetch:&error]) { /*this is the offending line*/
		// Update to handle the error appropriately.
		NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
		exit(-1);  // Fail
	}
    
}
Whitehk is offline   Reply With Quote
Old 07-09-2011, 06:10 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

core data to many sorting on fetch - Stack Overflow
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-09-2011, 07:31 PM   #5 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Thanks for the reference. I'll take a look at it and see what I can do.
Whitehk is offline   Reply With Quote
Old 07-09-2011, 07:56 PM   #6 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Alright. So I looked over it, and I'm still not understanding something important. Why is a to-many key not allowed in my fetchedResultsController method? It seems to be something wrong with the sort descriptors because whenever I change

Code:
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"classDay.dayName" ascending:YES];
to sort by the course name rather than the classDay name with this:

Code:
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
It doesn't crash. How come I can't sort by the classDay.dayName key?

Last edited by Whitehk; 07-09-2011 at 07:57 PM. Reason: typo
Whitehk is offline   Reply With Quote
Old 07-09-2011, 08:10 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Did you try the solution as described in the link?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 07-09-2011, 09:11 PM   #8 (permalink)
Registered Member
 
Whitehk's Avatar
 
Join Date: Feb 2010
Posts: 119
Whitehk is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Did you try the solution as described in the link?
Yes. I got the same results. However in the example you gave me, wouldn't the Person entity be akin to my ClassDay entity and the Event entity to my Course entity? If that's the case, they seem to be fetching the ClassDay Entity. I want to fetch the Course entity. I'm looking more for what the user Wienke said:

Quote:
If I understand correctly, you want to fetch "event" (course) entities and sort them according to their eventTitle (course name) attribute. So you would set up your sort descriptors for those entities the same as you would for your to-one fetches, since eventTitle (course name) is a to-one attribute. You'd then access the "person" (ClassDay) entities through the retrieved "event" (course) entities. – Wienke
Here's what I came up with in my code:

Code:
NSSortDescriptor *classDaySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"classDay.dayName" ascending:YES];
    NSSortDescriptor *courseSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:classDaySortDescriptor, courseSortDescriptor, nil];
    
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Course" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    [fetchRequest setSortDescriptors:sortDescriptors];
    [fetchRequest setFetchBatchSize:20];
    
    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = theFetchedResultsController;
    fetchedResultsController.delegate = self;
    
    [classDaySortDescriptor release];
    [courseSortDescriptor release];
    [sortDescriptors release];
    [fetchRequest release];
    [theFetchedResultsController release];
Whitehk is offline   Reply With Quote
Reply

Bookmarks

Tags
advanced, core data, entities, model, relationships

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 360
10 members and 350 guests
apatsufas, chemistry, lendo, leostc, Leslie80, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:35 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0