last day (15 days later) » 

11:46 AM
0
Q: RestKit relationships not mapping when manually mapping JSON object

davidethellI am following some suggested posts on how to map incoming JSON data directly using RKMapperOperation. My entity object is being created in the data store, but without the proper relationships. Interestingly, if I create an object directly using the Core Data methods after I've already mapping in...

 
Show the JSON. Is there a reason you're using the operation instead of an object manager? The question also doesn't show the link between the two pieces of code you show...
 
I have expanded the question with the JSON data and how the function calls are used.
 
So it seems your problem is really that you want stub objects to be created, if the objects don't already exist, for the ids so you can connect the relationships and then fill in the details later?
 
Actually, the attendee object and session object referred to by the attendee_id and session_id respectively will always exist prior to the checkin. Those records always exist before the object comes in over web sockets. I'll explore sending them in the JSON anyway but it will more than double the data size coming through which just seems inefficient considering I already have the records locally.
 
You don't need to send them nested if you already have them. Are you sure they're persisted to disk before you run this part?
 
11:46 AM
The existing attendee and session records are definitely persisted to disk. They came through as part of the login to the app.
Not sure if this gives a clue, but creating a record locally only "fixes" the last record that came through the websocket mechanism. So if I have several records with disconnected relationships and then create a Checkin locally, only the last disconnected record gets fixed.
 
These are 1-to-many relationships, and things are being disconnected when a new thing is added?
 
Yes, there are many checkins to 1 session and many checkins to 1 attendee. A new checkin comes through websockets and I parse the JSON and map the result. The result is correctly mapped to a checkin entity but with the "session" and "attended" relationships nil.
It appears that my incoming entity isn't really saving to the persistent store correctly.
 
ok looking
Where do I set the assignmentPolicy? In the RKEntityMapping?
 
you need to connect the relationship using an instance of RKRelationshipMapping
 
12:16 PM
ok, trying this for now:
let attendeeRelationship = RKRelationshipMapping(fromKeyPath: "checkin", toKeyPath: "attendee", withMapping: self.attendeeMapping())
checkinMapping.addPropertyMapping(attendeeRelationship)
This didn't seem to help. I did the same thing with the session relationship. Maybe the fromKeyPath is wrong altogether since there really isn't a key path in the JSON that corresponds to the relationship.
Maybe the issue isn't the mapping at all.
The behavior I'm seeing is that nothing is really persisted to the disk store.
For example, I'm running the simulator and after the incoming JSON and mapping I examine the .sqlite file and nothing is in it for the new record.
However, if I navigate out of my current view and back in the UITableView that uses a fetched results controller "sees" the new record, but the record isn't persisted to storage. Once I quit the app an restart the new record is gone
So something is wrong in my code for persisting the incoming data. It is in memory, but not persisted to disk even thought I'm using the persistent store context for my mapping operation.
Thanks for helping on this. It seemed pretty straightforward with the RestKit docs so I'm sure I'm just missing something simple.
 
yeah, I think this will need to be a custom mapping, drilling into the individual ID and using a nil mapping to extract it to match as the id of the destination object
you shouldn't look directly at the SQL file, query the DB if you want to know what's there
 
won't the sql file show the data if I call saveToPersistentStore?
The app shows the data but when I quit and restart the data is gone.
 
why are you using the persistent context directly?
you should be using a child, probably of the main thread context, and saving that up to the persistent store
the SQL file is split in parts, it will be there somewhere, but where...
 
2 reasons: the examples I saw and I wanted to be sure the data was there in case of an app crash
 
the persistent context should be considered private
 
12:25 PM
Most of the examples for mapping from JSON directly showed using the persistent store.
I also tried creating a new context that was a child of the main object context.
Like this SO example here:
0
A: Restkit: Duplicate objects getting created when performing my own RKMapperOperation

panupanThis is the solution we went with. Ensure identificationAttributes have been set in the mapping. Use RKMappingOperation without setting its destinationObject and RestKit will try to find an existing entity to map to by its identificationAttributes. We're also using RKFetchRequestManagedObjectCach...

That answer used RKMappingOperation instead of RKMapperOperation so I abandoned that approach but I could go back to at least using that method of creating a context
But I don't think that solves my relationship issue.
However, it does appear the relationship is working once I navigate away from the view and return.
 
It just doesn't see it when I reload the table data while still in that view.
I'll give it another go with a child context that is a private type from the mainobjectcontext and see where I get.
 
I'd suggest starting with a simple test on the context, take out the mapping and explicitly create a sample object and save it
 
Thanks again for sticking with this one.
 
get the context part working first
be sure to call saveToPersistentStore: on your context to save it
 
12:32 PM
yeah. The entire app up to this point completely relies on RestKit to do all the work with getObject, putObject and postObject calls so until we added the websocket code there was no issue.
 
so you know you have a good basis there, create new context from the store and use that
 
So is there any need to call performBlockAndWait outside the RKMapperOperation?
Doesn't the mapper take care of concurrency issues?
 
IIRC the RKManagedObjectMappingOperationDataSource takes care of it
but I rarely have cause to use these classes directly
 
Me either.
I must be doing something wrong the main thread because adding a new context now throws the saveToPersistentStore call into a wait trap
 
paste the code for creating the context and test object
 
12:45 PM
    let mapperContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.PrivateQueueConcurrencyType)
    mapperContext.parentContext = self.objectStore.mainQueueManagedObjectContext
    mapperContext.mergePolicy = NSOverwriteMergePolicy
let mappingDS = RKManagedObjectMappingOperationDataSource(managedObjectContext: mapperContext, cache: RKFetchRequestManagedObjectCache())
 
ahh, the dangers of cutting and pasting another SO answer! :)
Trying this now.
 
you shouldn't really need to set any attrs on the context and when you save it should update the main thread context automatically
(using this approach)
 
Well, it didn't give a deadlock and it does create a Checkin entity object, but still with no relationships.
ok, I need to run to a meeting so that will have to do for now. You've at least advanced my knowledge of rest kit immensely. Thanks for your help.
I may just punt for now since I'm getting the "checkin" object and just look up the attendee and session objects myself and assign them to the newly mapped checkin object. Not ideal as I'm also trying to use this mapping code generically for other places that will be receiving objects via websockets.
 
ok, good luck :)
 

  last day (15 days later) »