NSArrayController bindings and preservesSelection
NSArrayController bindings and preservesSelection
- Subject: NSArrayController bindings and preservesSelection
- From: Michael Link <email@hidden>
- Date: Sat, 30 Aug 2008 22:00:09 -0500
I have a situation where I have a source list whose selection controls
the bindings of an NSArrayController, and thus the content of a view.
Depending on what source a user selects in the source list the
NSArrayController can have its MOC bound and a fetch predicate set or
its contentSet bound to an NSObjectController who is bound to the MOC
with a fetch predicate set (ideally I'd be using strictly fetch
predicates which would solve this problem, but it seems that binding
the array controller to the object controller and setting the
contentSet key path to selection.toManyRelationship is * faster than
the equivalent fetch predicate even though it does the same thing).
When the source list selection is changed so that the array
controllers bindings are changed from contentSet to MOC then the array
controller does preserve the selection.
If the array controllers bindings are changed from a MOC to a
contentSet the array controller doesn't seem to preserve the selection
even though the new content also contains objects that were in the
selection. I'd like to find a way that the selection can be preserved
with this change, if possible?
This is the code that manages this process...
@implementation MXBindHistoryItem
@synthesize bindingsInfo = _bindingsInfo;
@synthesize fetchPredicate = _fetchPredicate;
- (id)initWithTitle:(NSString*)title bindings:
(NSDictionary*)bindingsInfo fetchPredicate:(NSPredicate*)predicate
{
if (self = [super initWithTitle:title]) {
_bindingsInfo = bindingsInfo;
_fetchPredicate = predicate;
}
return self;
}
- (NSDictionary*)_bindingsInfoForController:(id)controller
{
NSMutableDictionary* __bindingsInfo = [NSMutableDictionary dictionary];
for (NSString* __binding in [controller exposedBindings]) {
NSDictionary* __bindingInfo = [controller infoForBinding:__binding];
if (__bindingInfo) {
[__bindingsInfo setObject:__bindingInfo forKey:__binding];
}
}
return __bindingsInfo;
}
- (void)_unbindController:(id)controller bindingsInfo:
(NSDictionary*)bindingsInfo
{
if ([bindingsInfo objectForKey:NSManagedObjectContextBinding]) {
[controller setAutomaticallyPreparesContent:NO];
for (NSString* __binding in bindingsInfo) {
[controller unbind:__binding];
}
// [controller setManagedObjectContext:nil];
// [controller setFetchPredicate:nil];
}
else {
for (NSString* __binding in bindingsInfo) {
[controller unbind:__binding];
}
}
}
- (void)_bindController:(id)controller
{
for (NSString* __binding in _bindingsInfo) {
NSDictionary* __bindingInfo = [_bindingsInfo objectForKey:__binding];
[controller bind:__binding toObject:[__bindingInfo
objectForKey:NSObservedObjectKey] withKeyPath:[__bindingInfo
objectForKey:NSObservedKeyPathKey] options:[__bindingInfo
objectForKey:NSOptionsKey]];
}
}
- (void)bindController:(id)controller
{
NSDictionary* __bindingsInfo = [self
_bindingsInfoForController:controller];
if (![__bindingsInfo count]) {
if ([_bindingsInfo objectForKey:NSManagedObjectContextBinding]) {
[self _bindController:controller];
[controller setAutomaticallyPreparesContent:YES];
[controller setFetchPredicate:_fetchPredicate];
[controller fetch:nil];
}
else {
[self _bindController:controller];
}
}
else {
if ([_bindingsInfo objectForKey:NSManagedObjectContextBinding]) {
if ([[_bindingsInfo objectForKey:NSManagedObjectContextBinding]
objectForKey:NSObservedObjectKey] == [[__bindingsInfo
objectForKey:NSManagedObjectContextBinding]
objectForKey:NSObservedObjectKey]) {
[controller setFetchPredicate:_fetchPredicate];
}
else {
[self _unbindController:controller bindingsInfo:__bindingsInfo];
[self _bindController:controller];
[controller setAutomaticallyPreparesContent:YES];
[controller setFetchPredicate:_fetchPredicate];
}
}
else {
[self _unbindController:controller bindingsInfo:__bindingsInfo];
[self _bindController:controller];
}
}
}
@end
_______________________________________________
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