Welcome, Guest
Username Password: Remember me

Proper configuration for my Core Data model
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Proper configuration for my Core Data model

Proper configuration for my Core Data model 1 year, 8 months ago #1

Hi,

I have a CD model which basically looks like this:

Person <--->> PersonInfo <<---> Group

A person can belong to one or more groups. This connection is established via PersonInfo objects which have a 1:n relationship to Person and Group. PersonInfo also stores a number representing the person's rank within the associated group.

I already managed to implement two views exactly like I want them. The first one is a simple list of all persons which lets the user add, delete and edit. This was easy.

The second view contains a list of all groups (with the usual editing capabilities). I've got the list working properly but now I encountered several problems with the detail view of a group:

1) I need two sections. The first one for editing the group name and the second one should be a list of all associated PersonInfo objects, displaying the persons' name. Essentially this is a view for adding and removing persons from a list. I have no idea how to configure the tableViewModel.

2) I'd like to let users sort persons within a group. The sort order is represented by the PersonInfo's rank attribute. As far as I understand it, SensibleTableView disables sorting for Core Data objects. How would I do that?

Thanks for any tipps and ideas!

Martin
  • mhoeller
  • OFFLINE
  • Fresh Boarder
  • Posts: 1
  • Karma: 0

Re: Proper configuration for my Core Data model 1 year, 8 months ago #2

Hi Martin:

Let me answer number two first, as it will aid in number one's answer.

2- Sensible TableView does not at all disable sorting. On the contrary, it gives you two sorting options:

a. Automatic sorting. If you create an SCArrayOfObjectsSection with the sectionWithHeaderTitle:withEntityClassDefinition:, STV will automatically sort all the objects with the entity defined in the SCClassDefinition, according to the SCClassDefinition "key" property. By default, the class definition's key property has the same value as the first attribute in the entity. For an example of this, please refer to the Core Data App sample application.

b. Manual Sorting. This method gives you the maximum flexibility, not just with sorting, but also with selecting any subset of the objects you have in your Core Data graph. With this option, you should usually create an SCArrayOfObjectsSection using the sectionWithHeaderTitle:withItems:withClassDefinition: method. Here is code sample that illustrates that:

 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:myEntity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"sortKey" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
 
NSMutableArray *sectionItems = [NSMutableArray arrayWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:NULL]];
 
[sortDescriptor release];
[sortDescriptors release];
[fetchRequest release];
 
SCArrayOfObjectsSection *objectsSection =
[SCArrayOfObjectsSection sectionWithHeaderTitle:nil withItems:sectionItems
withClassDefinition:myClassDefinition];
 


Now for question number 1:

1- I am assuming you now have an automatically generated detail view with all the group attributes, and all you need is add an extra section with the persons associated with this group. If I understand you correctly, I think all you need to do is add a new SCArrayOfObjectsSection to the automatically generated view, using very similar code to the code above. You can have access to all automatically generated detail view model's using the SCTableViewModelDelegate method: tableViewModel:detailViewWillAppearForSectionAtIndex:withDetailTableViewModel:

Here is some sample code to illustrate this:

 
- (void)tableViewModel:(SCTableViewModel *)tableViewModel
detailViewWillAppearForSectionAtIndex:(NSUInteger)index withDetailTableViewModel:
(SCTableViewModel *)detailTableViewModel
{
NSFetchRequest *myFetchRequest = ... // Create your customized fetch request here
 
NSMutableArray *sectionItems = [NSMutableArray arrayWithArray:
[self.managedObjectContext executeFetchRequest:myFetchRequest error:NULL]];
 
[myFetchRequest release];
 
SCArrayOfObjectsSection *objectsSection =
[SCArrayOfObjectsSection sectionWithHeaderTitle:nil withItems:sectionItems
withClassDefinition:myClassDefinition];
 
[detailTableViewModel addSection:objectsSection];
}
 


Hope this helps
  • tarekskr
  • OFFLINE
  • Administrator
  • Posts: 2402
  • Karma: 72

Re: Proper configuration for my Core Data model 1 year, 7 months ago #3

Hi Tarek, this -- manual sort -- is exactly what I want to do as well but I'm having a little trouble implementing it in the context of my project, since I need it to apply to two different entities (although with the same fetch request and sortation). If you still have a copy of my project, it's the list views of the Chapter and Content objects that I need to custom sort (the lists of user-created objects, that is, not the detail list of each object's attributes).

In addition, I need to 'read' the new arrangement once the user has manually rearranged the lists back into the displayOrder key in my entities, although I'm fairly certain of how to do that I need to plug into the proper delegate when the user hits the 'done' editing button. This will make the user re-ordering in the list views 'sticky', and give me a preset sortation when I extract the data from the model for export. Any help you could give me would be much appreciated!
  • Geoffrey
  • OFFLINE
  • Senior Boarder
  • Posts: 48
  • Karma: 3
Last Edit: 1 year, 7 months ago by Geoffrey. Reason: Update
  • Page:
  • 1
Time to create page: 2.20 seconds