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
@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?
@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
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:
I have the following ImportableUniqueObject extensions
and
And finally my import code is
...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
@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?
@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
@JohnEstropia commented on GitHub (Dec 10, 2017):
Wow, lucky guess :D Cheers!