Re: NSArrayController loses selection upon CoreData refreshObject.
Re: NSArrayController loses selection upon CoreData refreshObject.
- Subject: Re: NSArrayController loses selection upon CoreData refreshObject.
- From: Quincey Morris <email@hidden>
- Date: Mon, 20 Jun 2011 00:36:11 -0700
On Jun 19, 2011, at 23:47, Motti Shneor wrote:
> Background: I use an NSTableView with an NSArrayController to display a list of CoreData entity instances (say instances of "Note" entity). I DO NOT configure the array controller for "Entity" mode, because this is a "read only" view. All model changes originate in remote server commands. My client application merely sends requests to the server, and server decides upon model changes.
>
> NSArrayController is configured for "Class" (NSManagedObject), and its ContentSet is bound to some relation property that leads to all the Notes.
This isn't correct. Entity mode has nothing really to do with whether the model is updatable or not, but rather specifies whether the data source has array semantics (Class) or set semantics (Entity). In the latter case, it also tells the array controller that the data model is a Core Data model.
Now, because NSArrayController is pretty much a black box, it's not at all clear whether the kind of mis-configuration you did has any important effect or not. Nevertheless, you should fix this. Note that the array controller has a separate "editable" checkbox which you should turn off, if you want to make sure it doesn't cause any model updates.
> The symptom is: When user clicks on a table line --- it gets selected. If, however, a server message arrives that causes the client to call
>
> [context refreshObject:obj] mergeChanges:YES];
>
> Selection disappears immediately. I double checked that NONE of the NSManagedObjects (Note instances) has changed, and no new Note was added, and no Note was removed. The refresh is used to update some Fetched properties (queries).
It's possible that the mergeChanges removes all objects from the relationship set, triggering a KVO notification to the array controller which causes it to clear the selection (because the set is empty), then adds objects back in. Or, it's possible that your array controller mis-configuration has a hand in this.
If you explicitly define a selection indexes property in your data model (which means a NSIndexSet* property in a subclass of the appropriate NSManagedObject class), you can bind the array controller's selection binding to this property***, then save and restore the selection indexes around the mergeChanges message. It's not clear from your description whether this *ought* to be necessary, but it's something you can try if you can't find a more direct solution.
*** What this does, in effect, is to cause the array controller to keep a mirror copy of the selection in the data model object. Because it's a binding, the selection can be set from either end. Just make sure your property implementation is KVO compliant, which is pretty simple in this case.
_______________________________________________
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