dingbat/nsrails — pull request #5 by jdjennin (2012-05-25 16:34:09+00:00)
Title: Added Core Data support to NSRails

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.