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
remoteIDis 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.remoteIDmust be defined in your *.xcdatamodeld data model file. You should create an abstract entity that defines aremoteIDattribute (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
idkey), 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.
-
– initInsertedInstantiates a new instance, inserts it into the default CoreData context.
-
– saveContextSave the CoreData object context of the receiver.
Methods to override
-
+ entityNameOverride this method if the class entity name is different than the name of the class.
-
– validatesRemoteIDUniquenessOverride this method only if you wish to disable
remoteIDuniqueness validation.
Class Methods
entityName
Override this method if the class entity name is different than the name of the class.
+ (NSString *)entityNameReturn 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.hfindObjectWithRemoteID:
Finds the object in CoreData whose remoteID is equal to the value passed in.
+ (instancetype)findObjectWithRemoteID:(NSNumber *)remoteIDParameters
- 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.hInstance Methods
initInserted
Instantiates a new instance, inserts it into the default CoreData context.
- (instancetype)initInsertedReturn 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.hsaveContext
Save the CoreData object context of the receiver.
- (NSError *)saveContextReturn Value
The error from the save, if one occurred.
Declared In
NSRRemoteManagedObject.hvalidatesRemoteIDUniqueness
Override this method only if you wish to disable remoteID uniqueness validation.
- (BOOL)validatesRemoteIDUniquenessReturn 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