Importing data #127

Closed
opened 2025-12-29 18:23:21 +01:00 by adam · 8 comments
Owner

Originally created by @ro22e0 on GitHub (Feb 19, 2017).

Does CoreStore support UserInfo Keys like MagicalRecord ?

Originally created by @ro22e0 on GitHub (Feb 19, 2017). Does CoreStore support [UserInfo Keys](https://github.com/magicalpanda/MagicalRecord/blob/master/Docs/Importing-Data.md#attributes) like MagicalRecord ?
adam closed this issue 2025-12-29 18:23:21 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Feb 20, 2017):

Sorry, not yet. This is actually currently being prototyped but there is no release schedule yet.

@JohnEstropia commented on GitHub (Feb 20, 2017): Sorry, not *yet*. This is actually currently being prototyped but there is no release schedule yet.
Author
Owner

@DimaAvvakumov commented on GitHub (Mar 28, 2018):

Hi! Thanks for awesome library.

One question - this feature still in prototype state?

@DimaAvvakumov commented on GitHub (Mar 28, 2018): Hi! Thanks for awesome library. One question - this feature still in prototype state?
Author
Owner

@JohnEstropia commented on GitHub (Mar 28, 2018):

@DimaAvvakumov Ah, I sort of abandoned this as I didn't like the complete magic string-dependency. Even released an architecture to get rid of xcdatamodels completely (CoreStoreObject.

Let me know if there is a significant benefit for your use-case with userInfo keys instead of the current ImportableObject method.

@JohnEstropia commented on GitHub (Mar 28, 2018): @DimaAvvakumov Ah, I sort of abandoned this as I didn't like the complete magic string-dependency. Even released an architecture to get rid of `xcdatamodel`s completely ([CoreStoreObject](https://github.com/JohnEstropia/CoreStore#type-safe-corestoreobjects). Let me know if there is a significant benefit for your use-case with userInfo keys instead of the current `ImportableObject` method.
Author
Owner

@DimaAvvakumov commented on GitHub (Mar 29, 2018):

Thank you for answer. UserInfo keys is not my primary goal. I want to custom mapping keys while importing object from json.

For example:
json

{
  "Id": 1,
  "FullName": "John Dow"
}

entity

class User {
  var id: Int = 0
  var name: String?
}

How can i import this?

@DimaAvvakumov commented on GitHub (Mar 29, 2018): Thank you for answer. UserInfo keys is not my primary goal. I want to custom mapping keys while importing object from json. For example: *json* ``` { "Id": 1, "FullName": "John Dow" } ``` *entity* ``` class User { var id: Int = 0 var name: String? } ``` How can i import this?
Author
Owner

@JohnEstropia commented on GitHub (Mar 29, 2018):

@DimaAvvakumov Check out the ImportableUniqueObject protocol:
https://github.com/JohnEstropia/CoreStore#importing-data

Simple example:

class User: NSManagedObject, ImportableUniqueObject {
    @NSManaged var id: Int
    @NSManaged var name: String?

    // MARK: ImportableUniqueObject
    typealias ImportSource = [String: Any]
    typealias UniqueIDType: Int

    class var uniqueIDKeyPath: String {
        return #keyPath(User.id) 
    }
    class func uniqueID(from source: ImportSource, in transaction: BaseDataTransaction) throws -> Int? {
        return source["id"] as? Int
    }
    func update(from source: ImportSource, in transaction: BaseDataTransaction) throws {
        self.name = source["name"] as? String
    }
}

You can then import your object via transactions:

CoreStore.perform(
    asynchronous: { (transaction) -> Void in
        let json: [String: Any] = // ...
        try! transaction.importUniqueObjects(
            Into<User>(),
            sourceArray: (json["users"] as? [User.ImportType]) // [[String: Any]]
        )
        // ...
    },
    completion: { _ in }
)
@JohnEstropia commented on GitHub (Mar 29, 2018): @DimaAvvakumov Check out the `ImportableUniqueObject` protocol: https://github.com/JohnEstropia/CoreStore#importing-data Simple example: ```swift class User: NSManagedObject, ImportableUniqueObject { @NSManaged var id: Int @NSManaged var name: String? // MARK: ImportableUniqueObject typealias ImportSource = [String: Any] typealias UniqueIDType: Int class var uniqueIDKeyPath: String { return #keyPath(User.id) } class func uniqueID(from source: ImportSource, in transaction: BaseDataTransaction) throws -> Int? { return source["id"] as? Int } func update(from source: ImportSource, in transaction: BaseDataTransaction) throws { self.name = source["name"] as? String } } ``` You can then import your object via transactions: ```swift CoreStore.perform( asynchronous: { (transaction) -> Void in let json: [String: Any] = // ... try! transaction.importUniqueObjects( Into<User>(), sourceArray: (json["users"] as? [User.ImportType]) // [[String: Any]] ) // ... }, completion: { _ in } ) ```
Author
Owner

@DimaAvvakumov commented on GitHub (Mar 29, 2018):

Thank you, I'll deal with this.

@DimaAvvakumov commented on GitHub (Mar 29, 2018): Thank you, I'll deal with this.
Author
Owner

@JohnEstropia commented on GitHub (Mar 29, 2018):

Sounds tough! 😆
Feel free to ask anything!

@JohnEstropia commented on GitHub (Mar 29, 2018): Sounds tough! 😆 Feel free to ask anything!
Author
Owner

@Pratik-Sodha commented on GitHub (May 13, 2019):

Thank you for awesome CoreData Wrapper.

I have situation in which i have to store pre-fill master data. Sync at app launch event. I have array of Model.

**For example : **

class ToyStory {
    var id: Int = 0
    var name: String?
    
    init(id : Int, name : String) {
        self.id = id
        self.name = name
    }
}

let modelsArray : [ToyStory] = [
    ToyStory(id: 1, name: "Sherrif Woody"),
    ToyStory(id: 1, name: "Buzz"),
    ToyStory(id: 1, name: "Andy"),
    ToyStory(id: 1, name: "Rex")
]

Thank you in advance :)

@Pratik-Sodha commented on GitHub (May 13, 2019): Thank you for awesome CoreData Wrapper. I have situation in which i have to store pre-fill master data. Sync at app launch event. I have array of Model. **For example : ** ``` class ToyStory { var id: Int = 0 var name: String? init(id : Int, name : String) { self.id = id self.name = name } } let modelsArray : [ToyStory] = [ ToyStory(id: 1, name: "Sherrif Woody"), ToyStory(id: 1, name: "Buzz"), ToyStory(id: 1, name: "Andy"), ToyStory(id: 1, name: "Rex") ] ``` Thank you in advance :)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#127