Restkit: Mapping parent reference to child object - restkit

Is it possible to write something similar to this code so that in the child objects I get a reference to parent object during the mapping process? Let's assume I have a child object with a property of type ParentClass called parent:
RKPropertyMapping* parentPropertyMapping =
[RKRelationshipMapping relationshipMappingFromKeyPath:#"#parent."
toKeyPath:#"parent"
withMapping:[mapping copy]];
[mapping addPropertyMapping:parentPropertyMapping];
I can later traverse from within the child objects to their parents.
UPDATE 1:
The code above is used by any entity in the hierarchy of JSON objects (except strings, numbers, etc). That is, it tries to set the linkage between the nested json objects and their owners (parents).
JSON response body:
response.body={"returnCode":{"messages":[],"name":"OK","returnCode":1}}
There is a ReturnCodeClass and a MessageClass both with a parent property. I'd like to have a reference (parent) pointing back to the owning (parent) object from these nested objects. E.g. from every message to the returnCode. In case of NSManagedObjects it is done automatically by the inverse relationship. But in case of NSObjects I try to emulate this behavior with using the #parent accessor.

Related

How should I add child element to parent object in react reducer

I've got a redux parent object that I'm building based on various api calls and used across components. When I receive new api data, I add a nested object to this parent component.
My current method deepClone's the parent object, adds the new child and then calls a reducer to update the store return { ...state, parent_object: action.value };
This works to an extent although mapStateToProps is not being called when the reducer updates. Also, deepCloning the parent object every time doesn't seem to make sense.
Could someone provide some insight into the proper way to do this.

Set a Parent property on Child Objects when binding

We have a hierarchical model of the shape (yuml-ized):
And a controller action that takes an Order-typed parameter:
public ActionResult UpdateOrder(Order order)
{
...
}
With the posted form having fields for the line items, too, we get a tree of objects from the MVC's model binding where as the binders create a new order object for each order to be bound.
The question: Is it possible to have the binding mechanism set the Parent property of each LineItem to the Order object they are being added too?
I take it that the binder that binds the Order object is responsible for executing a binder for each child object, too, by calling BindModel on the child object binders. We already have custom implementations (inheriting from DefaultModelBinder) in place: A OrderBinder as well as a LineItem binder.
How can our LineItem.BindModel method find out whether the LineItem being bound is "stand-alone" or about to be added to an Order whose binding is in progress, and if the latter is the case, how do we get the reference to that Order?

Deleteing a managed object from an entity

I've got a managedObjectContext with 2 entities, each of which contains multiple entries. Objects of Entity A are represented in a Table View, and I want the user to be able to delete any entity is Entity A. My problem is that, when I send the request to delete that entry, the wrong entity is being called!
FYI, I’m handling the deleting process in a separate method, so when the delete button in Table view is triggered, before it’s been taken out of the view, I first want to make sure it’s been removed from the managedObjectContext.
- (BOOL) deleteCompletedSuccessfully : (EntityA *) anEntry
{
[self.managedObjectContext deleteObject: anEntry];
NSError *error = nil;
If (![self.managedObjectContext save:&error])
{
NSLog (#”%#”, [error userInfo]);
return NO;
}
return YES;
}
The error is:
Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)…. And the rest of the error message indicates that I’m trying to delete a nil object in EntityB!!!! While anEntry is in fact in EntityA.
I tried encapsulating the input (anEntry is this case) into an array, i.e:
- (BOOL) deleteCompletedSuccessfully : (NSArray *) array
{
EntryA *anEntry = [array objectAtIndex: 0];
// and the rest of the code
The same error. How can I make it look for that particular entry in a particular entity?!
Any help?
I think you've got some conceptual confusion going on here between entities and managed objects.
Entities are abstractions analogous to Classes. Managed objects are actual individual instances of NSManagedObject or one of its subclasses. Entities in the data model tell the managed object context what attributes and relationships to one another the managed object instances will have.
Entities exist solely in the data model whereas Managed objects are in the "object graph" which is the actual functioning group of related objects alive in memory. Entities simply describe to the managed object context how everything fits together much as a class definition tells the compiler how all the properties and behaviors of fit together in a class. Managed objects as instances have data and behaviors just like all other live objects.
Likewise, a managed object context does not add, remove or set the values of entities in any way. Instead, it adds, removes or set the values of managed objects configured by the entities in it's data model.
So, when you say:
I've got a managedObjectContext with 2
entities, each of which contains
multiple entities.
What you really mean is:
I've got a data model with 2 entities and a managed object context with many managed objects configured by those entities.
A tableview may display only the data from the instances configured to one entity (that is most common) but the actual data and the insertions and deletion happen to managed object instances and not the entities which are unalterable by that point.
However, I don't think the terminology confusion is the actual cause of your problem. Instead, I think the error is trying to tell you that you are deleting an object configured by EntityA from a required relationship with a an object configured with entityB.
The cocoa error 1570 is a NSValidationMissingMandatoryPropertyError which as the name suggest occurs when you try to save a managed object that has a required property with a nil value. The manage object context tries to validate the object graph before it saves and when it finds a missing required property it throws that error.
I can't tell you anything more because I have no idea what your data model looks like.

Core Data: Instantiating a "root object" in a document based application

I am creating a document based project using Core Data and have run into what may simply be a conceptual issue for me, as while I am not new to Cocoa, this is my first attempt to utilize Core Data. What I am trying to accomplish should be relatively simple: with each new document launched, I would like a new instance of one of my model objects created that serves as a "root" object.
What I have done is add an NSObjectController to my xib, set its mode to Entity Name (with the correct entity name provided), checked off "Prepares Content", and bound its managed object context to File's Owner with managedObjectContext as the model key path. To test this, I bound the title of my main window to the object controller, with controller key as selection and model key path as one of the keys in my entity.
I know I can create my root object programmatically, but am trying to adopt the mediator pattern as is recommended by Apple. I have seen the instructions in the department-employee tutorial under the "adopting the mediator pattern" section and the steps detailed are exactly what I believe I have done.
Any thoughts?
Edit:
Perhaps I did not state the problem correctly. The models are created in Core Data and the relationships are setup as I need them to be (with a "root", children and leaves, using to-one parent relationships, to-many children relationships and an isLeaf boolean attribute). My issue is ensuring that this root object is instantiated as a singleton every time a new document is launched. There should be exactly a 1:1 relationship between the root object and the current document, that root object must always exist and be available without any user interaction to create it, and child nodes that are created and attached to the root are the data objects that are used and manipulated by the application.
I have implemented the above functionality programatically, but in keeping with Core Data principles, would like to adopt the mediator pattern completely and not manage any creation of data objects within my application logic.
If you want a "root" managed object like you would find in linked-list or tree, then you have to set that up in data model itself.
By default, a Core Data data model has no particular hierarchy among objects. Objects may be related but no object is logically "above" or "below" another one. You can reach in object in any relationship by starting with any other object and walking the relationship/s back to the desired object.
A hierarchy of managed objects needs a tree like structure like this:
Tree{
nodeName:string
parent<-->>Tree.children
children<<-->Tree.parent
}
... so that the "root" object is the sole Tree instances that has parent==nil.
Having said all this, I would point out that the Apple docs you refer to say that it is best NOT to use this type of built in hierarchy for most cases. It's just a simplification used for purposes of demonstration (and I think it is a bad one.)
The data model is intended to model/simulate the real-world objects, conditions or events that the app deals with. As such, the logical relationships between the entities/objects in the model/graph should reflect the real-world relationships. In this case, unless the real-world things you are modeling exist in a hierarchy with a real-world "root" object, condition or event, then your model shouldn't have one either.

What Query/Path-Language is used for References in Ecore-derived XMI-Instances?

Assume that I have an Ecore-model containing a package and some classes that make reference to each other. If i create a "Dynamic Instance", Eclipse produces an XMI-file and I can instantiate some classes. Containment-relations are directly serialized to an XML-tree in the XMI (the children elements in the example). But if I instantiate references to elements that are already contained somewhere in the tree, the Editor writes Path-Expressions like in the following, for the currentChild attribute:
<parent currentChild="//#parent/#children.1">
<children/>
<children/>
</parent>
As far as I know this is not XPath, because:
The "childrens" are elements not attributes and have not to be referenced via "#"
XPath uses the e.g., elem[1] and not elem.1 to get e.g., the second elem of a list
What is it and where can I find a information on it? I already tried to browse the EMF pages/specs but could not find it.
It's an EMF Fragment Path. The Javadoc describes it like this:
String org.eclipse.emf.ecore.InternalEObject.eURIFragmentSegment(EStructuralFeature eFeature, EObject eObject)
Returns the fragment segment that, when passed to eObjectForURIFragmentSegment, will resolve to the given object in this object's given feature.
The feature argument may be null in which case it will be deduced, if possible. The default result will be of the form:
"#feature-name[.index]"
The index is used only for many-valued features; it represents the position within the list.
Parameters:
eFeature the feature relating the given object to this object, or null.
eObject the object to be identified.
Returns:
the fragment segment that resolves to the given object in this object's given feature.

Resources