I’m facing something mysterious. I have a boolean property on an NSManagedObject subclass “ReinspectionTask”. The property is “isOpen”, indicating whether the task still needs to be completed. In Core Data, the attribute type is set to Boolean. The property is declared as NSNumber. I pull JSON from the server and it looks like this:
[
{
"jobID":"73",
"address":"500 17th Ave. ",
"city":"Santa Cruz",
"state":"California",
"inspections":[
{
"inspectionID":"186",
"inspectionType":"Foundation",
"mediaKey":"777",
"hasTasks":true,
"requestSubmitted":false,
"reinspectionTasks":[
{
"taskID":"75",
"title":"Foundation re-inspection item",
"requestText":"Need a photo of <something>",
"isOpen":false
},
"wasApproved":false,
"isScheduled":false,
"hasAvailableTimes":false,
"availableTimes":[]
]
}
]
}
]
Then I map the JSON to my managed object model using RestKit with the following code:
RKManagedObjectMapping *jobMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Job"];
RKManagedObjectMapping *inspectionMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Inspection"];
RKManagedObjectMapping *reinspectionTaskMapping = [RKManagedObjectMapping mappingForEntityWithName:@"ReinspectionTask"];
jobMapping.primaryKeyAttribute = @"jobID";
[jobMapping mapKeyPath:@"address" toAttribute:@"address"];
[jobMapping mapKeyPath:@"city" toAttribute:@"city"];
[jobMapping mapKeyPath:@"state" toAttribute:@"state"];
[jobMapping mapKeyPath:@"jobID" toAttribute:@"jobID"];
[jobMapping mapRelationship:@"inspections" withMapping:inspectionMapping];
[[RKParserRegistry sharedRegistry] setParserClass:[[RKParserRegistry sharedRegistry]
parserClassForMIMEType:RKMIMETypeJSON]
forMIMEType:@"text/html"];
inspectionMapping.primaryKeyAttribute = @"inspectionID";
[inspectionMapping mapKeyPathsToAttributes:
@"inspectionID", @"inspectionID",
@"inspectionTime", @"inspectionTime",
@"inspectionType", @"inspectionType",
@"inspector", @"inspector",
@"isScheduled", @"isScheduled",
@"mediaKey", @"mediaKey",
@"requestSubmitted", @"requestSubmitted",
@"hasAvailableTimes", @"hasAvailableTimes",
@"availableTimes", @"availableTimes",
@"hasTasks", @"hasTasks",
@"wasApproved", @"wasApproved", nil];
[inspectionMapping mapRelationship:@"reinspectionTasks" withMapping:reinspectionTaskMapping];
[inspectionMapping mapRelationship:@"job" withMapping:jobMapping];
reinspectionTaskMapping.primaryKeyAttribute = @"taskID";
[reinspectionTaskMapping mapKeyPath:@"taskID" toAttribute:@"taskID"];
[reinspectionTaskMapping mapKeyPath:@"title" toAttribute:@"title"];
[reinspectionTaskMapping mapKeyPath:@"requestText" toAttribute:@"requestText"];
[reinspectionTaskMapping mapKeyPath:@"isOpen" toAttribute:@"isOpen"];
[reinspectionTaskMapping mapRelationship:@"inspection" withMapping:inspectionMapping];
[objectManager.mappingProvider setMapping:jobMapping forKeyPath:@"job"];
[objectManager.mappingProvider setMapping:inspectionMapping forKeyPath:@"inspection"];
[objectManager.mappingProvider setMapping:reinspectionTaskMapping forKeyPath:@"reinspectionTasks"];
This all works fine for the most part. However, I’m having an issue with the isOpen BOOL property on ReinspectionTask. When I log the value to the console, it returns null. What is strange is that, as far as I can tell, I have set up that property just like hasTasks, isScheduled, etc. Those BOOL properties work fine.
I should mention that the relationship of Inspection to ReinspectionTasks is a to-many relationship. Does that kind of relationship require that I take some other step in setting up the object mapping in RestKit?
Or might it have to do with the fact that the ReinspectionTask object is nested deeper in the JSON structure?
Any thoughts or suggestions are more than welcome. Thanks very much.
Well there are only a two questions that are quite easy to answer.
No, you seem to be doing it correctly.
No, this is not the problem. As I mentioned in a comment above, I suspect you are doing something wrong. A BOOL property should never appear in the console as null. I would check all the properties on your target objet and check they are the correct type in your datamodel and in your entity class (if you have one). I would also check the default value for this property in your datamodel