ImportableUniqueObject
public protocol ImportableUniqueObject : ImportableObject, Hashable
NSManagedObject subclasses that conform to the ImportableUniqueObject protocol can be imported from a specified ImportSource. This allows transactions to either update existing objects or create new instances this way:
class Person: NSManagedObject, ImportableObject {
typealias ImportSource = NSDictionary
typealias UniqueIDType = NSString
// ...
}
dataStack.perform(
asynchronous: { (transaction) -> Void in
let json: NSDictionary = // ...
let person = try transaction.importUniqueObject(
Into<Person>(),
source: json
)
// ...
},
completion: { (result) in
// ...
}
)
-
The data type for the entity’s unique ID attribute
Declaration
Swift
associatedtype UniqueIDType : ImportableAttributeType -
The keyPath to the entity’s unique ID attribute
Declaration
Swift
static var uniqueIDKeyPath: String { get } -
The object’s unique ID value. The default implementation returns the value of the attribute pertained to by
uniqueIDKeyPathImportant
It is the developer’s responsibility to ensure that the attribute value pertained byuniqueIDKeyPathis notnilduring the call touniqueIDValue.Declaration
Swift
var uniqueIDValue: UniqueIDType { get set } -
Return
trueif an object should be created fromsource. Returnfalseto ignore and skipsource. The default implementation returns the value returned by theshouldUpdate(from:in:)implementation.Declaration
Swift
static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> BoolParameters
sourcethe object to import from
transactionthe transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
Return Value
trueif an object should be created fromsource. Returnfalseto ignore. -
shouldUpdate(from:Default implementationin: ) Return
trueif an object should be updated fromsource. Returnfalseto ignore and skipsource. The default implementation returnstrue.Default Implementation
Declaration
Swift
static func shouldUpdate(from source: ImportSource, in transaction: BaseDataTransaction) -> BoolParameters
sourcethe object to import from
transactionthe transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
Return Value
trueif an object should be updated fromsource. Returnfalseto ignore. -
Return the unique ID as extracted from
source. This method is called beforeshouldInsert(from:in:)orshouldUpdate(from:in:). Returnnilto skip importing fromsource. Note that throwing from this method will cause subsequent imports that are part of the sameimportUniqueObjects(:sourceArray:)call to be cancelled.Declaration
Swift
static func uniqueID(from source: ImportSource, in transaction: BaseDataTransaction) throws -> UniqueIDType?Parameters
sourcethe object to import from
transactionthe transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
Return Value
the unique ID as extracted from
source, ornilto skip importing fromsource. -
Implements the actual importing of data from
source. This method is called just after the object is created and assigned its unique ID as returned fromuniqueID(from:in:). Implementers should pull values fromsourceand assign them to the receiver’s attributes. Note that throwing from this method will cause subsequent imports that are part of the sameimportUniqueObjects(:sourceArray:)call to be cancelled. The default implementation simply callsupdate(from:in:).Declaration
Swift
func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throwsParameters
sourcethe object to import from
transactionthe transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
-
Implements the actual importing of data from
source. This method is called just after the existing object is fetched using its unique ID. Implementers should pull values fromsourceand assign them to the receiver’s attributes. Note that throwing from this method will cause subsequent imports that are part of the sameimportUniqueObjects(:sourceArray:)call to be cancelled.Declaration
Swift
func update(from source: ImportSource, in transaction: BaseDataTransaction) throwsParameters
sourcethe object to import from
transactionthe transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
View on GitHub
ImportableUniqueObject Protocol Reference