using KVC key paths in bindings
Posted 18 October 2011 - 04:16 AM
This is not an example from my actual application, but a cut-down version to show the salient points.
In this example, I have a Recipe and and Ingredient entity, and a bridging entity to perform the many-to-many (see attached core data screen shot
I want to show a RecipeDetailViewController that has the Recipe's properties, followed by a section that shows all its Ingredients. In the 2nd image here, I showed a mockup.
In this example I used a custom cell which pulls in different fields from the RecipeIngredient table and from the Ingredient table. Tapping one of these cells should open the RecipeIngredient detail editor, and tapping the disclosure button should open the Ingredient detail editor.
I would like to be able refer to all these bindings using KVC paths, so for bound fields in the 2nd section, I would like to use a binding on the RecipeIngredient object with the following key paths:
I realise I could write custom code to do this and pull in the name, but I have a model that will define these fields to bind as KVC paths, and so it would be great if I could just pass it through to the underlying method which already supports this. Since the keypath can traverse a to-one relationship already, i would like to be able to just bind using this path and have the data get faulted in if required.
This example uses a custom cell, but I would also like more trivial examples to work with key paths, perhaps a Recipe has a creator relationship to a Person and a person has a fullName property. I would like to be able to bind a field to creator.fullName and have it displayed in the table (probably just read only). I think I could do this by using an ObjectCell, but would it be possible to set read-only cells using the path?
Posted 20 October 2011 - 03:44 PM
Thank you very much for the detailed explanation!
I think the easiest way to approach this is for us to extend the "intermediate entity" support in STV to have it automatically generate the intermediate detail views for you and have it tied to both entities. We can have this ready in STV 2.2, due in roughly a month from now. Does this time frame suit your deadlines for this project? Thanks again for all your feedback.
Posted 20 October 2011 - 09:58 PM
Thanks for looking into it. I do think adding intermediateEntity support to the array of object section would be a good idea, but as you mentioned in another post, there are possible several issues that come up. I do think the above usage of disclosure buttons works very neatly with Apple's intended use of these buttons, and could be generically applied by STV in these cases though.
This feature is not urgent for me, and in the meantime i will just use an array of objects pointing to my intermediate entity, and require the user to navigate through the intermediate to get to the far end of the many-to-many relationship. The timeframe for 2.2 will be fine (and I'm happy to have further discussions on how the intermediate entity could work).
What I am trying to solve urgently is how to be able to use key values to address attributes, rather than the core data attribute names. This is similar to what I was talking about above. I'm trying to work out the best way to do this without having to modify any STV code.
I was under the impression that you can only use core data attributes when binding objects with core data, but now I think the best approach is using SCCustomPropertyDefinitions to define these KVC-driven properties. Other ideas I had were subclassing the arrayofobjectssection to override isValidPropertyName to always return true when a boolean ivar called allowExtensibleProperties or similar is true.
This is a follow-on from the discussion here, and you might prefer to reply there instead:
The essence of what I'm trying to do is use STV with core data backed objects but be able to specify property names that do not exist in core data, but that KVC can resolve for me if I use valueForKeyPath. I realise I could just use a DictionaryDefinition and KVC but I think STV provides some context saving/deleting logic that I would miss out on. The most elegant thing I can think of at the moment is subclassing SCCustomPropertyDefinition to create a KVCPropertyDefinition which allows any property to be defined on any object, and when the property value is evaluated, valueForKey provides the binding using the property name (which could be something like "ingredient.name" or "someKeyThatDoesntExistInCoreData". I think this would work for both core data objects and normal objects, and provided the key exists it will be a valid property name.
What do you think? Like I said, one way or the other I need to solve this (with or without STV) by Monday. I'd like to make it as loosely coupled to the STV library as possible (or even contribute a new class for you to add in a future revision)
Posted 23 October 2011 - 05:31 PM
I can give you a working pre-release of our next version in about a week from now. Would that be ok with your deadline? Thanks!
What I am trying to solve urgently is how to be able to use key values to address attributes, rather than the core data attribute names.
Posted 23 October 2011 - 08:31 PM
I think that would be great. I will probably have to do something quick and dirty this week (possibly a hack at the 2.1.7 code) to get something up and running, and can refactor it next week or when I can get a look at the next release of STV. You've answered the main question, which is that this will be available in STV, just not right now. I don't mind having forked code for a week or so, but I wouldn't want this to be my long term solution - I don't want to miss out on all the future updates from STV!
Posted 23 October 2011 - 09:14 PM
A quick hack would be to subclass SCClassDefinition and disable validation altogether by overriding the 'isValidPropertyName' method (always return TRUE). I can't guarantee however that this will work flawlessly as we've never tested this before. Hope this helps!
Posted 28 November 2011 - 01:43 AM
I'm still interested in trialling this before it's released if possible - let me know if there is a beta in a testable state. Thanks
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users