Re: Core-data binding to all entities
Re: Core-data binding to all entities
- Subject: Re: Core-data binding to all entities
- From: Martin Hewitson <email@hidden>
- Date: Fri, 9 Oct 2009 19:32:32 +0200
In the end I was able to solve this by making an NSArrayController
bound to all entities of the type Note, then set the Filter Predicate
of that array controller depending on the switch state. So I have:
- (IBAction)allCategoriesSelected:(id)sender
{
[singleCategoryButton setState:0];
[allCategoriesButton setState:1];
[allNotesArrayController setFilterPredicate:nil];
}
- (IBAction)singleCategorySelected:(id)sender
{
[allCategoriesButton setState:0];
[singleCategoryButton setState:1];
NSTreeNode *node = [[treeController selectedNodes] firstObject];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"category.name
== %@", [[node representedObject] valueForKey:@"name"]];
[allNotesArrayController setFilterPredicate:pred];
}
This works fine, though I wonder what happens in terms of performance
if the number of Note entities gets very large.
Kind regards,
Martin
On Oct 2, 2009, at 4:37 PM, Stamenkovic Florijan wrote:
Martin,
I am actually working on an identical app (as far as your
description goes), as a way of learning CoreData, but also to have
something to organize my todos and reminders in...
I am dealing with the same problem you describe below, though I took
a slightly different approach. I bind the contentSet to the selected
Group's notes relationship, but when I unbind it I do not manually
fetch all the notes like you do. Instead, my notesController is set
to fetch them itself, so I just need to invoke the fetch method.
However, for some reason the "canRemove" property of the notes
controller turns to NO after I unbind the contentSet, so I also can
not delete notes... What is weird is that this only happens after I
have had the notesController bound over the groupsController during
a run of the app. It seems that switching the content like this is
not well supported, and tends to confuse the controller. In that
sense I will change my code to perform filtering of notes based on
the selection in the outline of groups, as opposed to using this
approach.
There is another problem with this approach... For more info see a
recent post of mine on the cocoa dev list titled: Dynamic
NSArrayContent changing [was: NSTableColumn value binding]
If you like, we can exchange our apps offlist, it might be
interesting to compare features etc.
Best regards,
F
-(void)setViewingAllNotes:(NSNumber*)newValue
{
// assign the new values
id oldValue = viewingAllNotes;
viewingAllNotes = [newValue copy];
[oldValue release];
// react appropriately
if([viewingAllNotes boolValue]){
// cache and then remove the current selection of the groups
controller
lastSelectedGroupsIndexPaths = [[groupsController
selectionIndexPaths] retain];
[groupsController setSelectionIndexPaths:nil];
// re-direct the bindings of the notesController
[notesController unbind:@"contentSet"];
[notesController fetch:self];
}else{
// re-direct the bindings of the notesController
[notesController bind:@"contentSet"
toObject:groupsController
withKeyPath:@"selection.items"
options:[NSDictionary dictionaryWithObject:[NSNumber
numberWithBool:YES]
forKey:NSDeletesObjectsOnRemoveBindingsOption]];
// restore and then release the last selection of the groups
controller
[groupsController
setSelectionIndexPaths:lastSelectedGroupsIndexPaths];
[lastSelectedGroupsIndexPaths release];
lastSelectedGroupsIndexPaths = nil;
}
}
On Oct 02, 2009, at 02:39, Martin Hewitson wrote:
Dear list,
I have a simple core-data model with an entity 'Category' and an
entity 'Note'. The 'Category' entity has a to-many relationship
'notes' to entity type 'Note'.
The categories are displayed in an outline view via a tree
controller. There is also a table which displays the contents of an
array controller (NotesArrayController) - this is supposed to show
a subset of the notes.
I have a switch on the UI which is meant to let the user choose
whether to display all notes in the currently selected category, or
all notes from all categories. So I want to modify the content of
NotesArrayController depending on the state of the switch.
So far I tried doing this programatically with bindings using the
two methods below (actually the switching mechanism is just two
buttons). This works as far as displaying the correct content to
the user, but when it comes to deleting objects when in 'all
categories' mode, it doesn't work properly. I get messages in the
console like:
*** -[NSCFArray removeObjectAtIndex:]: mutating method sent to
immutable object
So that's pretty clear, and I'm obviously going about this in the
wrong way. The question is, what is a proper way to handle this?
Thanks in advance for any constructive comments,
Martin
#pragma mark -
#pragma mark Category control
- (IBAction)allCategoriesSelected:(id)sender
{
[singleCategoryButton setState:0];
[allCategoriesButton setState:1];
[notesArrayController unbind:@"contentSet"];
[notesArrayController unbind:@"contentArrayForMultipleSelection"];
[notesArrayController setContent:nil];
NSManagedObjectContext *moc = managedObjectContext;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Note"
inManagedObjectContext:moc]];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];
if (error) {
[NSApp presentError:error];
[request release];
return;
}
[notesArrayController setContent:results];
[request release];
}
- (IBAction)singleCategorySelected:(id)sender
{
[allCategoriesButton setState:0];
[singleCategoryButton setState:1];
[notesArrayController unbind:@"contentSet"];
[notesArrayController unbind:@"contentArrayForMultipleSelection"];
[notesArrayController setContent:nil];
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSNumber
numberWithBool:YES]
forKey:NSDeletesObjectsOnRemoveBindingsOption];
// bind contents to entries in the selected categories
[notesArrayController bind:@"contentSet"
toObject:treeController
withKeyPath:@"selection.notes" options:dict];
// so that multiple selected categories works properly but this is
not really
// necessary since we don't allow multiple selection on the
outline view at the
// moment.
[notesArrayController bind:@"contentArrayForMultipleSelection"
toObject:treeController
withKeyPath:@"email@hidden"
options:nil];
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Martin Hewitson
Albert-Einstein-Institut
Max-Planck-Institut fuer
Gravitationsphysik und Universitaet Hannover
Callinstr. 38, 30167 Hannover, Germany
Tel: +49-511-762-17121, Fax: +49-511-762-5861
E-Mail: email@hidden
WWW: http://www.aei.mpg.de/~hewitson
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden