Trent
Trent

Reputation: 1109

Optimal way to organise JSON for IOS parser

I'm using NSJSONSerialization to parse some JSON from my web service, and I want to know the most efficient way to lay it out.

Currently I generate the JSON in this format:

[
  {"title":"title Val","description":"description val","appointmentDate":"2012-06-16T00:00:00"},
  {"title":"title Val","description":"description val","appointmentDate":"2012-06-16T00:00:00"},
  {"title":"title Val","description":"description val","appointmentDate":"2012-06-16T00:00:00"}
]

The parser constructs an NSArray where each element is an NSDictionary containing the appointment details (title, description, date).

Now my application actually needs to search the parsed JSON for appointments with dates between some interval so it can show them based on some user action. Currently I just scan the array and retreive any appointments that meet the condition. This is of course O(n). Should I be representing my JSON differently when I want to search on a particular field? Perhaps like this?

{
  "2012-06-16T00:00:00" : {"title":"title Val","description":"description val"},
  "2012-06-18T00:00:00" : {"title":"title val","description":"description val"},
  "2012-06-20T00:00:00" : {"title":"title val","description":"description val"},
}

This way the parser will generate an NSDictionary where each row's key is the appointment date. Is it more efficient to do this, or can I perform a binary search on an NSArray of NSDictionary objects just as quickly? If the array was searched, it would be necessary to do a [row objectForKey:@"appointmentDate"] in the comparison predicate which could potentially be an expensive operation.

Upvotes: 0

Views: 140

Answers (2)

aroth
aroth

Reputation: 54856

What about parsing out the appointments and storing them using something like Core Data, with indices set on the fields you want to search over? That might be simpler and faster than trying to optimize and search manually.

Though if you want to do it manually then yes, building a sorted index keyed by the information you want to search over will give you a more efficient search. So you could have your dictionary that maps NSDate's (don't keep the data in a string format) to the set of appointments on that date, and then you can perform a binary-search over the keyset of the dictionary (make sure it's properly sorted first!) to identify the range of keys that fall within your search interval. Then you can go into the dictionary and fetch the corresponding appointments.

Upvotes: 1

EricS
EricS

Reputation: 9768

If the appointments are in order, just do a binary search rather than a linear one. Unless you have tens of thousands of items, it will likely be more than fast enough.

One thing that can cost you a lot of time is re-parsing the dates, though. NSDates are slow to create, so I suggest either creating them once when you read-in the list or convert the strings to numeric values, such as seconds since 1970, which are fast to compare.

Upvotes: 1

Related Questions