Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow late arriving child records to be related to correct parent #234

Closed
wants to merge 1 commit into from

3 participants

@itsniper

When a parent record of a to-one or to-many relationship arrives before it's child, an empty child object is created. When the child record arrives, another new object is created with no way to relate it to it's parent. By adding the primary key value when creating missing child objets they can then be found and updated when the child record arrives.

@itsniper

Looks like Issue #223 was opened for this.

@blackgold9
Collaborator

Hoping to get a unit test for this one. I just pushed a commit that should make writing tests easier (tests will now compile)

@itsniper

OK, cool! Got stuck doing planning all last week, but hopefully will have a chance to take another look this week. Took a quick look late that night we talked, and it may just be the way the JSON I'm getting is formatted. I'll let you know though. Thanks!

@itsniper

OK, so this ended up being an issue with the JSON. It was a to-one relationship, but in the JSON it only contained a single number for the ID of the child. From Saul's post on CIMGF I noticed that it seemed to only work that way if the value was an array of IDs, and it was a to-many relationship. The server-side is being developed in-house, so I was able to get them to "correct" the JSON.

Old (incorrect) JSON were "host" is the to-one relationship:
{
"sev":4,
"id":1,
"host":10,
"status":2
}

New (working) JSON:
{
"sev":4,
"id":1,
"host":
{
"id":10
},
"status":2
}

@itsniper itsniper closed this
@Glnn

I'd say this is pretty essential stuff!

Actually I need to reverse this commit. Totally forgot it was up on GitHub. The issue ended up being that my JSON was incorrectly formatted. This fixed it for my JSON format, but I have no idea what else it might have broken. I just changed my JSON format and the original code works fine. If you're seeing this issue check your JSON against the unit test examples in the project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 10, 2012
This page is out of date. Refresh to see the latest.
View
4 MagicalRecord/Categories/NSManagedObject/NSManagedObject+MagicalDataImport.m
@@ -212,6 +212,9 @@ - (BOOL) MR_importValuesForKeysWithObject:(id)objectData
{
NSEntityDescription *entityDescription = [relationshipInfo destinationEntity];
relatedObject = [entityDescription MR_createInstanceInContext:[self managedObjectContext]];
+ NSString *primaryKey = [relationshipInfo MR_primaryKey];
+ id relatedValue = [localObjectData MR_relatedValueForRelationship:relationshipInfo];
+ [relatedObject setValue:relatedValue forKey:primaryKey];
}
[relatedObject MR_importValuesForKeysWithObject:localObjectData];
@@ -231,6 +234,7 @@ + (id) MR_importFromObject:(id)objectData inContext:(NSManagedObjectContext *)co
if (managedObject == nil)
{
managedObject = [self MR_createInContext:context];
+ [managedObject setValue:value forKey:[primaryAttribute name]];
}
[managedObject MR_importValuesForKeysWithObject:objectData];
Something went wrong with that request. Please try again.