NSRRemoteManagedObject Class Reference
Inherits from | NSRRemoteObject : _NSR_SUPERCLASS |
Declared in | NSRRemoteManagedObject.h |
Overview
Setting up
Installation
CocoaPods: Instead of
pod 'NSRails'
, usepod 'NSRails/CoreData'
.If you’re otherwise including NSRails in a workspace with your project, link with the static library created by the NSRailsCD target, rather than just NSRails.
Why is a separate target necessary?
In the NSRails target, NSRRemoteObject inherits from NSObject. But because your CoreData-managed, NSRails-enabled class need to inherit from NSManagedObject, and because Objective-C does not allow multiple inheritance, NSRRemoteObject inherits from NSManagedObject in the CoreData target. NSRRemoteManagedObject is a subclass of NSRRemoteObject with nice CoreData behavior.
Setting a universal context
You must set your managed object context to your config’s managedObjectContext property so that NSRails can automatically insert or search for CoreData objects when operations require it:
[[NSRConfig defaultConfig] setManagedObjectContext:<#your MOC#>];
Remote ID
remoteID
is used as a “primary key” that NSRails will use to find other instances, etc. This key also validates uniqueness, meaning saving a context with two records of the same type with the same remoteID will fail.remoteID
must be defined in your *.xcdatamodeld data model file. You should create an abstract entity that defines aremoteID
attribute (Integer 16 is fine) and acts as a parent to your other entities:
Differences from NSRRemoteObject
NSRRemoteManagedObject overrides some NSRRemoteObject behavior to make it more CoreData-friendly.
Remote requests
Class
Remote All (index): Each object returned in the array may be an existing or newly inserted managed object. All managed objects will reflect properties set to those returned by your server. Does not save the context.
Object with ID: If request is successful, will attempt to find an existing local object with objectID, and update its properties to the server’s response. If it cannot find an existing local object with that remoteID, will insert a new object into the context, with those properties. Does not save the context.
Instance
Fetch: If successful and changes are present, will save its managed object context.
Create: If successful, will save its managed object context to update changed properties in the server response (like remoteID).
Destroy: If successful, will delete itself from its managed object context and save the context.
Update/Replace: If successful, will save its managed object context. Note: Changes to the local object will remain even if the request was unsuccessful. It is recommended to implement an undo manager for your managed object context to rollback any changes in this case.
Others
objectWithRemoteDictionary: Will attempt to retrieve the object in CoreData whose remoteID matches the object for key id
in dictionary.
- If this object is found, will set its properties using dictionary.
- If this object is not found (or there’s no
id
key), will create & insert a new object using dictionary.
Does not save the context.
remoteDestroyOnNesting: This property leaves your managed object unaffected. You will have to delete it from your context manually if your request was successful.
nestedClassForProperty: Will search your CoreData entity relationships, and if one is found for that property, will return that relationship’s destination class.
Tasks
CoreData Helpers
-
+ findObjectWithRemoteID:
Finds the object in CoreData whose remoteID is equal to the value passed in.
-
– initInserted
Instantiates a new instance, inserts it into the default CoreData context.
-
– saveContext
Save the CoreData object context of the receiver.
Methods to override
-
+ entityName
Override this method if the class entity name is different than the name of the class.
-
– validatesRemoteIDUniqueness
Override this method only if you wish to disable
remoteID
uniqueness validation.
Class Methods
entityName
Override this method if the class entity name is different than the name of the class.
+ (NSString *)entityName
Return Value
String (typically literal) to be used as entity name when inserting and fetching objects of this class.
Discussion
Default behavior here is to return the name of the class.
Declared In
NSRRemoteManagedObject.h
findObjectWithRemoteID:
Finds the object in CoreData whose remoteID is equal to the value passed in.
+ (instancetype)findObjectWithRemoteID:(NSNumber *)remoteID
Parameters
- remoteID
The remoteID to search for.
Return Value
The object from CoreData, if it exists. If it does not exist, returns nil
.
Declared In
NSRRemoteManagedObject.h
Instance Methods
initInserted
Instantiates a new instance, inserts it into the default CoreData context.
- (instancetype)initInserted
Return Value
The newly inserted object.
Discussion
Does not save the context.
Uses the “global” context defined in the relevant config’s managedObjectContext
property. Throws an exception if this property is nil
.
Declared In
NSRRemoteManagedObject.h
saveContext
Save the CoreData object context of the receiver.
- (NSError *)saveContext
Return Value
The error from the save, if one occurred.
Declared In
NSRRemoteManagedObject.h
validatesRemoteIDUniqueness
Override this method only if you wish to disable remoteID
uniqueness validation.
- (BOOL)validatesRemoteIDUniqueness
Return Value
NO
if you do not wish to validate remoteID
uniqueness. You should not override this method otherwise.
Discussion
Unfortunately CoreData does not offer a good way to validate on uniqueness (on the DB level). When enabled and an object is inserted, NSRails will make a fetch request for any other managed objects with the inserted object’s remoteID
. This may take time depending on the amount of records you have.
Declared In
NSRRemoteManagedObject.h