Re: NSArrayController bindings and preservesSelection
Re: NSArrayController bindings and preservesSelection
- Subject: Re: NSArrayController bindings and preservesSelection
- From: Michael Link <email@hidden>
- Date: Sat, 30 Aug 2008 22:22:04 -0500
Doh! I must not have had my afternoon hit of caffeine. I was still
using fetch: instead of fetchWithRequest:merge:error: for the object
controller which caused the contentSet to be nil (and thus the
selection to be wiped out) until the fetch was sent the next time
around the run loop.
--
Michael
On Aug 30, 2008, at 10:00 PM, Michael Link wrote:
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