Importing Array #139

Closed
opened 2025-12-29 15:25:23 +01:00 by adam · 5 comments
Owner

Originally created by @martheli on GitHub (May 27, 2017).

I have an array like this:

let array = ["John", "Maria", "Erika", "Susan"]

And I would like this array to be imported into my Persons entity. I have read the README in regards to importing arrays and I am not sure if just using the below is sufficient?

      CoreStore.perform(
                     asynchronous: { (transaction) -> Void in
                    try! transaction.importObject(
                        Into<Person>(),
                        source: Array
                )
        },
            completion: { _ in }
        )

I am getting "Ambiguous reference to member importObject" error. Am I supposed to add the following class to my project before the import works?

public protocol ImportableObject: class {
    typealias ImportSource
    static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool
    func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws
}
Originally created by @martheli on GitHub (May 27, 2017). I have an array like this: `let array = ["John", "Maria", "Erika", "Susan"]` And I would like this array to be imported into my Persons entity. I have read the README in regards to importing arrays and I am not sure if just using the below is sufficient? ``` CoreStore.perform( asynchronous: { (transaction) -> Void in try! transaction.importObject( Into<Person>(), source: Array ) }, completion: { _ in } ) ``` I am getting "Ambiguous reference to member importObject" error. Am I supposed to add the following class to my project before the import works? ``` public protocol ImportableObject: class { typealias ImportSource static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws } ```
adam added the question label 2025-12-29 15:25:23 +01:00
adam closed this issue 2025-12-29 15:25:23 +01:00
Author
Owner

@JohnEstropia commented on GitHub (May 28, 2017):

Yes, you will need to implement ImportableObject or ImportableUniqueObject methods first.
You can only import from the type you declare ImportSource as. For example, if you set

typealias ImportSource = [String: Any]
func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws {
    self.name = source["name"] as! String?
}

you can import a single object with

try transaction.importObject(
    Into<Person>(),
    source: ["name": "John"]
)

and multiple objects with

try transaction.importObjects(
    Into<Person>(),
    sourceArray: [
        ["name": "John"],
        ["name": "Maria"],
        ["name": "Erika"],
        ["name": "Susan"]
    ]
)
@JohnEstropia commented on GitHub (May 28, 2017): Yes, you will need to implement `ImportableObject` or `ImportableUniqueObject` methods first. You can only import from the type you declare `ImportSource` as. For example, if you set ```swift typealias ImportSource = [String: Any] func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws { self.name = source["name"] as! String? } ``` you can import a single object with ```swift try transaction.importObject( Into<Person>(), source: ["name": "John"] ) ``` and multiple objects with ```swift try transaction.importObjects( Into<Person>(), sourceArray: [ ["name": "John"], ["name": "Maria"], ["name": "Erika"], ["name": "Susan"] ] ) ```
Author
Owner

@martheli commented on GitHub (May 28, 2017):

Whenever I use the following, I get error that says "Typealias is missing an assigned type":

typealias ImportSource = [String: Any]

public protocol ImportableObject: class {
    typealias ImportSource
    static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool
    func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws
}
@martheli commented on GitHub (May 28, 2017): Whenever I use the following, I get error that says "Typealias is missing an assigned type": ``` typealias ImportSource = [String: Any] public protocol ImportableObject: class { typealias ImportSource static func shouldInsert(from source: ImportSource, in transaction: BaseDataTransaction) -> Bool func didInsert(from source: ImportSource, in transaction: BaseDataTransaction) throws } ```
Author
Owner

@JohnEstropia commented on GitHub (May 28, 2017):

The typealias needs to be implemented in your class. Please read the Swift documentation on associatedtype.

I would suggest you familiarize yourself first with Swift and Core Data in general as CoreStore's documentation assumes you have sufficient background on both.

@JohnEstropia commented on GitHub (May 28, 2017): The typealias needs to be implemented in your class. Please read the [Swift documentation on `associatedtype`](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Generics.html#//apple_ref/doc/uid/TP40014097-CH26-ID189). I would suggest you familiarize yourself first with Swift and Core Data in general as CoreStore's documentation assumes you have sufficient background on both.
Author
Owner

@martheli commented on GitHub (May 29, 2017):

Do any of the demos that you attached, include a sample of how to import data from JSON?

@martheli commented on GitHub (May 29, 2017): Do any of the demos that you attached, include a sample of how to import data from JSON?
Author
Owner

@JohnEstropia commented on GitHub (May 29, 2017):

Sorry, there's no demo code for importing yet. But the Unit tests do have a sample code you can refer to: https://github.com/JohnEstropia/CoreStore/blob/develop/CoreStoreTests/ImportTests.swift#L1068

By the way, for general questions/discussions you may want to join the CoreStore Slack group:
http://swift-corestore-slack.herokuapp.com/

@JohnEstropia commented on GitHub (May 29, 2017): Sorry, there's no demo code for importing yet. But the Unit tests do have a sample code you can refer to: https://github.com/JohnEstropia/CoreStore/blob/develop/CoreStoreTests/ImportTests.swift#L1068 By the way, for general questions/discussions you may want to join the CoreStore Slack group: http://swift-corestore-slack.herokuapp.com/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#139