-
Notifications
You must be signed in to change notification settings - Fork 37
Added Core Data support to NSRails #5
Conversation
…ails into core_data
…e a new Core Data entity for that relationship
… string for notification for context saving
…om within NSRailsManagedObject
This looks fantastic. Thanks so much, this is really appreciated. Of course there've been a bunch of changes since your fork, so it may take a bit of time to merge. In the meantime, do you think you could start adding some tests? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious - what was the reason for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the toy project I was using to implement this, the camelize function was turning location_id into locationId, if I remember correctly. I suppose I could have adjusted the property names on my iOS models. The shop I work at doesn't really standardize its naming conventions. Some of us use camel case and others use snake case. Most of us snake it. Feel free to change it back, though!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. And by the way, what's wrong with using the NSRails-given remoteID
property as the primary key? It's there exactly so you don't have to do location_id :)
And definitely yes on the tests. It'll probably be Tuesday before I can start that. Camping trip this weekend :-) On May 25, 2012, at 10:25 PM, dan hassinreply@reply.github.com wrote:
|
Enjoy! I guess this can wait until you get back then, but I had a few questions:
|
It's thread-specific though, so it's something the user should handle. The real problem is that even though the changes aren't saved, they are still there and will be saved the next time the altered object's managed object context is saved. Undoing will remove the changes so that the object is in its last saved state before being changed.
As long as there is a concise way to isolate the appropriate objects from the Core Data store for CRUD operations, it doesn't really matter to me how it's done :) |
Re: 2) Cool, I see. I'll add that to the remoteUpdate docs. Re: 3) Yes, remoteID would have to be defined in each CoreData model. This makes sense to me though - in your case you'd have to define class_id anyway. |
I think everything works. Don't worry about adding tests, I'll write them. |
Two last questions on
|
|
Alright, released NSRails 1.1, which included a lot of changes inspired by your work. A few differences from your commits, if you're interested:
That should be it. Let me know if you have any more suggestions or PRs, you should file them in separate issues. Thanks for your efforts! |
@jdjennin @dingbat (PS Daniel, I started using CoreData. Thanks for the shove in the right direction.) |
@marklarr Glad I could convert you :) Please open a new issue, and could you provide some more details? For what key is it crashing, are you setting an object's collection or an object's parent/container? |
@dingbat it looks like I may have had my core data model set up incorrectly. I just got home and it's working all of a sudden! As always, thanks for the quick response. |
I created an NSRailsManagedObject object by copying/pasting the code from NSRailsModel. NSRailsManagedObject, however, inherits from NSManagedObject rather than NSObject. When the CRUD and fetch operations succeed, the corresponding objects in Core Data (uniquely identified by their primary key attribute) are updated, and the Core Data managed object context is saved.
The primary key attribute is assumed to be classname_id in the Core Data model. So for a class named "User", the primary key would be "user_id".
Setup requires that the entities which should inherit from NSRailsManagedObject have their class set in the right pane of Xcode's data model editor (in the *.xcdatamodeld file) to the entity name. Otherwise, Core Data assumes that the entity's class inherits from NSManagedObject directly, and the application will crash.
remoteCreate will create the object locally and send a request to the server to create the object.
remoteUpdate will attempt to update the server's record, and if the request succeeds, the object will be saved locally. Otherwise, changes will be rolled back if you have implemented an undo manager for the core data managed object context.
remoteDestroy will send a request to the server to destroy the remote object. If successful, the local object will be deleted from Core Data.
remoteFetch will update the attributes of the object in Core Data and save it, if successful.