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 as NSUserDefaults.

    Declaration

    Swift

    associatedtype ImportSource
  • shouldInsert(from:in:) Default implementation

    Return true if an object should be created from source. Return false to ignore and skip source. The default implementation returns true.

    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 from source. Return false to ignore.

  • Implements the actual importing of data from source. Implementers should pull values from source and assign them to the receiver’s attributes. Note that throwing from this method will cause subsequent imports that are part of the same importObjects(: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.