ImportableUniqueObjects from nested JSON #195

Closed
opened 2025-12-29 15:26:29 +01:00 by adam · 3 comments
Owner

Originally created by @lepunk on GitHub (Dec 10, 2017).

Hi, this is driving me crazy. Not sure if its a bug in CoreStore or I'm doing something wrong (most likely its me)

I have 2 entities: Coin and Exchange. Coin has an "exchanges" relationship to many Exchange

My json looks something like this:

{
	"coins": [{
		"name": "Aircoin",
		"symbol": "AIR",
		"exchanges": [{
			"id": 26,
			"rank": 34,
			"name": "Yobit"
		}, {
			"id": 66,
			"rank": 75,
			"name": "Novaexchange"
		}],
		"id": 3072
	}]
}

I have the following ImportableUniqueObject extensions

extension Coin: ImportableUniqueObject {

    public typealias UniqueIDType = Int32
    public typealias ImportSource = JSON
    
    public static var uniqueIDKeyPath: String {
        return #keyPath(Coin.id)
    }
    
    public static func uniqueID(from source: JSON, in transaction: BaseDataTransaction) throws -> Int32? {
        return source["id"].int32!
    }
    
    public func update(from source: JSON, in transaction: BaseDataTransaction) throws {
        self.id = source["id"].int32!
        self.symbol = source["symbol"].string!
        self.exchanges = try! NSSet(array: transaction.importUniqueObjects(Into<Exchange>(), sourceArray: source["exchanges"].array!))
    }
}

and

extension Exchange: ImportableUniqueObject {
    
    public typealias UniqueIDType = Int32
    public typealias ImportSource = JSON
    public static var uniqueIDKeyPath: String {
        return #keyPath(Exchange.id)
    }
    
    public static func uniqueID(from source: JSON, in transaction: BaseDataTransaction) throws -> Int32? {
        return source["id"].int32!
    }
    
    public func update(from source: JSON, in transaction: BaseDataTransaction) throws {
        self.id = source["id"].int32!
        self.name = source["name"].string!
    }
}

And finally my import code is

let json = try JSON(data: r.content!)

if let coins = json["coins"].array {
    CoreStore.perform(
        asynchronous: { (transaction) -> Void in
            let _ = try! transaction.importUniqueObjects(
                Into<Coin>(),
                sourceArray: coins // make sure this is of type Array<MyPersonEntity.ImportSource>
            )
        },
        completion: { _ in
            print(self.getCoinCount())
            print(self.getExchangeCount())
            NotificationCenter.default.post(name: Notification.Name("coins_refreshed"), object: nil)
        }
    )
}

...where JSON is a SwiftyJSON object

My problem is that this code is not importing relationships correctly. If I print out the exchanges length in the Coin entity's update method it prints the correct value.

However if later on I fetch the Coin and print out the relationship's length its invalid and it varies each time i re-sync

Any help is appreciated and thanks for the amazing library

Originally created by @lepunk on GitHub (Dec 10, 2017). Hi, this is driving me crazy. Not sure if its a bug in CoreStore or I'm doing something wrong (most likely its me) I have 2 entities: Coin and Exchange. Coin has an "exchanges" relationship to many Exchange My json looks something like this: ``` { "coins": [{ "name": "Aircoin", "symbol": "AIR", "exchanges": [{ "id": 26, "rank": 34, "name": "Yobit" }, { "id": 66, "rank": 75, "name": "Novaexchange" }], "id": 3072 }] } ``` I have the following ImportableUniqueObject extensions ``` extension Coin: ImportableUniqueObject { public typealias UniqueIDType = Int32 public typealias ImportSource = JSON public static var uniqueIDKeyPath: String { return #keyPath(Coin.id) } public static func uniqueID(from source: JSON, in transaction: BaseDataTransaction) throws -> Int32? { return source["id"].int32! } public func update(from source: JSON, in transaction: BaseDataTransaction) throws { self.id = source["id"].int32! self.symbol = source["symbol"].string! self.exchanges = try! NSSet(array: transaction.importUniqueObjects(Into<Exchange>(), sourceArray: source["exchanges"].array!)) } } ``` and ``` extension Exchange: ImportableUniqueObject { public typealias UniqueIDType = Int32 public typealias ImportSource = JSON public static var uniqueIDKeyPath: String { return #keyPath(Exchange.id) } public static func uniqueID(from source: JSON, in transaction: BaseDataTransaction) throws -> Int32? { return source["id"].int32! } public func update(from source: JSON, in transaction: BaseDataTransaction) throws { self.id = source["id"].int32! self.name = source["name"].string! } } ``` And finally my import code is ``` let json = try JSON(data: r.content!) if let coins = json["coins"].array { CoreStore.perform( asynchronous: { (transaction) -> Void in let _ = try! transaction.importUniqueObjects( Into<Coin>(), sourceArray: coins // make sure this is of type Array<MyPersonEntity.ImportSource> ) }, completion: { _ in print(self.getCoinCount()) print(self.getExchangeCount()) NotificationCenter.default.post(name: Notification.Name("coins_refreshed"), object: nil) } ) } ``` ...where JSON is a SwiftyJSON object My problem is that this code is not importing relationships correctly. If I print out the exchanges length in the Coin entity's update method it prints the correct value. However if later on I fetch the Coin and print out the relationship's length its invalid and it varies each time i re-sync Any help is appreciated and thanks for the amazing library
adam closed this issue 2025-12-29 15:26:29 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Dec 10, 2017):

@lepunk Just a hunch, but have you checked that your relationships have the inverse relationship set on both entities?

@JohnEstropia commented on GitHub (Dec 10, 2017): @lepunk Just a hunch, but have you checked that your relationships have the inverse relationship set on both entities?
Author
Owner

@lepunk commented on GitHub (Dec 10, 2017):

@JohnEstropia you are my hero. spent most of my weekend figuring it out. can't believe the solution was this easy. thanks a bunch

@lepunk commented on GitHub (Dec 10, 2017): @JohnEstropia you are my hero. spent most of my weekend figuring it out. can't believe the solution was this easy. thanks a bunch
Author
Owner

@JohnEstropia commented on GitHub (Dec 10, 2017):

Wow, lucky guess :D Cheers!

@JohnEstropia commented on GitHub (Dec 10, 2017): Wow, lucky guess :D Cheers!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#195