1

I read this question and don't get answers

I Have multiple JSONs

RouteSequence

{
    "checkpoint_id": 145,
    "hash": "d5b335dac7c34ba1fd001268b80b21dd",
    "is_end": true,
    "is_forward_direction": true,
    "next_checkpoint_id": 144,
    "order": 0,
    "path": [
            "65.597615 57.153329",
            "65.597668 57.153424",
            "65.597659 57.153654",
            "65.596790 57.153182",
            "65.596679 57.153128",
            "65.595181 57.152339",
            "65.592321 57.150904",
            "65.591666 57.150562"
    ],
    "route_id": 87
},
.....

Route

{
    "active_from": "2014-03-25",
    "active_to": "2024-01-01",
    "capacity_type": "",
    "checkpoints_ids": [
            319,
            321,
            338,
            339,
            340,
            ......
            3355,
            3375
    ],
    "city_id": 1,
    "description": "Областная библиотека - с/о Липовый остров",
    "has_cars_for_disabled_persons": false,
    "hash": "49587333a979a22d93d09e4313661eaa",
    "id": 125,
            "name": "156",
    "route_type_id": 3
},
.......

Checkpoint

{
    "city_id": 2,
    "code_number": "09080",
    "description": "Большая, 183 в сторону Калинина",
    "hash": "cd398f0d4a7baa5a47facb3476394d84",
    "id": 1503,
    "lat": 56.1307455464724,
    "lon": 69.528181378059,
    "name": "2 микрорайон",
    "routes_ids": [
            401,
            873
    ]
},
.........

Entities picture and graph view of entities

I'm using something like this in loop for creating objects from json array:

+ (instancetype)instanceWithEntity:(NSEntityDescription*)entity inContext:(NSManagedObjectContext *)context fromJSONObject:(NSDictionary *)jsonData{
    Route *route = [[Route alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
    route.id = [jsonData valueForKey:@"id"];
    route.capacityType = [jsonData valueForKey:@"capacity_type"];
    route.objectHash = [jsonData valueForKey:@"hash"];
    route.sequenceDescription = [jsonData valueForKey:@"description"];
    route.name = [jsonData valueForKey:@"name"];
    return route;
}

And main question: How to handle relationships in these situation. My app at launch start to process this json files for every entity. The reason why i asking is that the my json is not fully nested, it has only arrays of ids of objects from other entities. So how to handle this? For example:

routeSequence.route = ?

this is relationship which refers to JSON RouteSequence. I can't just create here the object, i need to refers to existed object by his id(which can not exist, because this is still not downloaded and parsed)

Community
  • 1
  • 1
Bergutov Ruslan
  • 157
  • 2
  • 14

1 Answers1

0

I'd use a framework to support this processing, like RestKit.

What you're doing is foreign key mapping - the process of taking one object, looking up another object for an id it holds and connecting them.

Basically you would import all of the objects first and include the ids of everything they connect to. Then you run a second time over the imported data to connect all the instances together.

The ids can be stored as transient attributes as you only need them to facilitate the connection processing (though you can store them persistently if you want).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • i don't wan't to use any frameworks in this case, i think that in this situation can be very clean solution. I found objc_setAssociatedObject method, i wondering, maybe i can use it – Bergutov Ruslan Dec 02 '15 at 05:26
  • You could, though dropping to that level isn't required and just leads to the code being a little less obvious – Wain Dec 02 '15 at 09:43
  • is this very bad idea if i save this arrays as appended string? And later if i need to get this data, i will split this string to arrays by @",". What pitfalls can be in this situation? I dropped every relationships – Bergutov Ruslan Dec 02 '15 at 09:55
  • If you're using core data you should use relations. String processing is time consuming. A framework could make this trivial for you to implement... – Wain Dec 02 '15 at 10:37