ImportableObject
public protocol ImportableObject : DynamicObject
NSManagedObject
and CoreStoreObject
subclasses that conform to the ImportableObject
protocol can be imported from a specified ImportSource
. This allows transactions to create and insert instances this way:
class Person: NSManagedObject, ImportableObject {
typealias ImportSource = NSDictionary
// ...
}
dataStack.perform(
asynchronous: { (transaction) -> Void in
let json: NSDictionary = // ...
let person = try transaction.importObject(
Into<Person>(),
source: json
)
// ...
},
completion: { (result) in
// ...
}
)
-
The data type for the import source. This is most commonly an json type,
NSDictionary
, or another external source such asNSUserDefaults
.Declaration
Swift
associatedtype ImportSource
-
shouldInsert(from:
Default implementationin: ) Return
true
if an object should be created fromsource
. Returnfalse
to ignore and skipsource
. The default implementation returnstrue
.Default 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. -
Implements the actual importing of data from
source
. 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 sameimportObjects(:sourceArray:)
call to be cancelled.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.