0
votes

newbie with restkit problems here :(

this is my json response for trades that belong to an exchange and a currency:

{
    "exchange": "symbol",
    "currency": "USD",
    "trades": [
        {
            "maxPrice": "684.00",
            "minPrice": "683.10",
            "price": "683.28",
            "timestamp": "1390451006",
            "volume": "1.0"
        }
    ]
}

I have an array of trades that belong to an exchange defined in the root of this response, trades can be 1..n.

My exchange class has 2 attributes

{
    "displayName": "name",
    "symbol": "symbol"
}

My currency class has 2 attributes.

{
    "symbol": "$",
    "code": "USD" 
}

A proper JSON response and a easyout would be

{
    "trades": [
        {
            "maxPrice": "684.00",
            "minPrice": "683.10",
            "price": "683.28",
            "timestamp": "1390451006",
            "volume": "1.0",
            "exchange": {
                "displayName": "name",
                "symbol": "symbol"
            },
            "currency": {
                "symbol": "$",
                "code": "USD"
            }
        }
    ]
}

since all these trade belong to a single exchange and a currency i specified them at the root of the JSON response, i'm lost as to how i can map these relationship in restkit.

My Entities

Exchange
-displayName
-symbol
-trades(Exchange -->>Trade | 1:n)

Currency
-code
-symbol
-trades(Currency -->> |1:n)

Trade
-maxPrice
-minPrice
-price
-tradeDate
-volume
-currency(Trade >-->Currency |n:1)
-exchange(Trade >-->Exchange |n:1)

Any pointers or ideas would be sincerely appreciated.

Thanks for your help.

1
So you have full control of the JSON? What is your data model (the classes that you want to map into) and the relationships inside it? To specify a mapping you need the source (JSON) and destination (model)... - Wain
Yes i have full control, used mogenrator to generate the underlying code for the core data model. added entity definitions. Thanks for your help. - user3296887

1 Answers

0
votes

Not clear why you show so may different forms of JSON. I assume they are different responses for different search type requests. Start with one response and get the mappings for that working first (probably the last JSON which is a simple list of trades).

None of your JSON contains unique identifiers - it really should. This allows you flexibility in how connections are made between objects and ensures that you don't get duplicated objects in the data store.

Your diagram may misrepresent the relationships, but they should be inverse of each other (so 1:n & n:1, not 1:1). This allows Core Data to maintain the relationships properly.

As your JSON is all nested the mapping is relatively simple. You should be using a combination of RKEntityMapping and addRelationshipMappingWithSourceKeyPath:mapping: (or RKRelationshipMapping if the source and destination key names don't match).