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
uniqueIDKeyPath
Important
It is the developer’s responsibility to ensure that the attribute value pertained byuniqueIDKeyPath
is notnil
during the call touniqueIDValue
.Declaration
Swift
var uniqueIDValue: UniqueIDType { get set }
-
Return
true
if an object should be created fromsource
. Returnfalse
to 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) -> Bool
Parameters
source
the object to import from
transaction
the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
Return Value
true
if an object should be created fromsource
. Returnfalse
to ignore. -
shouldUpdate(from:
Default implementationin: ) Return
true
if an object should be updated fromsource
. Returnfalse
to ignore and skipsource
. The default implementation returnstrue
.Default Implementation
Declaration
Swift
static func shouldUpdate(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool
Parameters
source
the object to import from
transaction
the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.
Return Value
true
if an object should be updated fromsource
. Returnfalse
to ignore. -
Return the unique ID as extracted from
source
. This method is called beforeshouldInsert(from:in:)
orshouldUpdate(from:in:)
. Returnnil
to 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
source
the object to import from
transaction
the 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
, ornil
to 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 fromsource
and 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) throws
Parameters
source
the object to import from
transaction
the 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 fromsource
and 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) throws
Parameters
source
the object to import from
transaction
the transaction that invoked the import. Use the transaction to fetch or create related objects if needed.