Reputation: 12875
Trying to connect two entities from my API from a single response.
{
"id": 4546,
"body": "Direct message",
"status": "received",
"from": {
"id": 723,
"signature": "Mr. Whatever"
},
"sent_at": "2014-06-05T21:33:15Z",
"sent_to": ...
}
The object is a Message
entity, the from
field is a nested Sender
entity. Here is the relevant relationship mapping:
RKEntityMapping *senderMapping = [RKEntityMapping mappingForEntityForName:@"Sender"
inManagedObjectStore:[AMModelManager sharedManager].managedObjectStore];
senderMapping.identificationAttributes = @[ @"remoteId" ];
[senderMapping addAttributeMappingsFromDictionary:@{
@"id" : @"remoteId",
@"signature" : @"signature"
}];
[messageMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"from"
toKeyPath:@"sender"
withMapping:senderMapping]];
The first time the objects are mapped, the relationship is not established. I see it being mapped in the log, but when I print any particular Sender
object, its messages
inverse relationship shows an empty array instead of a relationship fault.
Mapped relationship object from keyPath 'from' to 'sender'. Value: <Sender: 0x16a10990> (entity: Sender; id: 0x16a14580 <x-coredata:///Sender/t16539BDF-5606-442B-9EE2-D88363FB04AD29> ; data: {
messages = (
);
remoteId = 723;
signature = "Mr. Whatever";
})
This makes the fetch requests I have in my application fail when I want to search for all messages from a given sender.
The next time I run the app and the mapping takes place again, all Message
and Sender
entities are remapped, the relationship is connected as expected, and my fetch requests behave as expected.
I really have no idea why the relationship isn't valid on the initial load. I don't know if it's a Core Data config issue or a RestKit issue, but it's driving me crazy and I would love any additional insight available. Glad to provide more information if necessary.
Edit
This is different than the proposed duplicate because this is a to-one
relationship, so the proposed answer of using RKAssignmentPolicyUnion
is inapplicable.
Update
Here is the fetch request I'm using for the controller I'm listening to:
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Sender"];
fetch.predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(SELF.messages, $message, $message.direct == %@).@count > 0 && SELF.remoteId != %@", @(YES), user.remoteId];
fetch.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"signature" ascending:YES selector:@selector(caseInsensitiveCompare:)]
];
Semantically I'm trying to say "Give me all the Senders that have sent a Message directly to the current user, and who are not the current user, sorted by their signature."
This fetch request fetches the Sender objects that I expect when I run it after my data is retrieved, but the controller fires no updates and my table remains empty until the next time I run the application.
Upvotes: 1
Views: 353
Reputation: 12875
K, another classic case of "You did it wrong and you were looking in all the wrong places to fix it."
I'd overridden the didChangeValueForKey:
method on my Message
entity's implementation to monitor some transient state, and neglected to call [super didChangeValueForKey:key]
, which fubar'd the inverse connection assignments in the mapping. Absolutely no issue with Core Data or RestKit. Just me being careless.
Upvotes: 2