Iglistkit: How to deal with JSON data?

Created on 20 Oct 2016  路  6Comments  路  Source: Instagram/IGListKit

Hi,
Firstly, I think this IGListKit is awesome and so useful!

This is more of a question than an issue, but I have JSON data returned from my servers that I want to populate my list with. Usually I would use SwiftyJSON to handle the data, and everytime -cellForItemAt is called, use something like data[indexPath.row]["text"].stringValue to retrieve the right part of the data and input it into the cell. However, I'm trying to figure out how to make this data work with IGListKit. As far as I can work out, I need to be able to some how separate each JSON object and combine it into an array that implements IGListDiffable to be returned in -objects(for:).

Would it be possible to provide me with some pointers, and possibly give an example of downloading and parsing JSON data from the web in the IGListKit example app? I think it would be super useful as I can see most people using this framework with data downloaded from online!

Thanks!

question

Most helpful comment

Hey @yusuftor ! 馃槃

I would definitely parse out the JSON into model objects. It sounds like you're just using raw JSON? That's probably not the best practice. 馃槄

The flow should be something like this:

  1. Network request
  2. Request completes, gives JSON to a parser object
  3. Parser returns array of parsed model objects (that conform to IGListDiffable)
  4. Return this array in -objects(for:)
  5. Then in your SectionController, you'll receive this model in -didUpdateToObject:

All 6 comments

Hey @yusuftor ! 馃槃

I would definitely parse out the JSON into model objects. It sounds like you're just using raw JSON? That's probably not the best practice. 馃槄

The flow should be something like this:

  1. Network request
  2. Request completes, gives JSON to a parser object
  3. Parser returns array of parsed model objects (that conform to IGListDiffable)
  4. Return this array in -objects(for:)
  5. Then in your SectionController, you'll receive this model in -didUpdateToObject:

@jessesquires Thanks a lot for your help and explaining. But would it be difficult for you, with all my respect, to show us a legit example with a simple json example and how can we make a model that conforms to IGListDiffable ?
Again, thanks a lot 馃槂

@kwstasna Check out the latest demo that we added for an example custom model that conforms to IGListDiffable:

https://github.com/Instagram/IGListKit/blob/master/Example/IGListKitExamples/ViewControllers/DiffTableViewController.swift#L18-L39

For doing JSON-to-Person and diffing, imagine something like:

let response: [ [String: Any] ] = // something from the web
var people = [Person]()
for json in response {
  if let pk = json["pk"] as? Int,
     let name = json["name"] as? String {
    people.append(Person(pk: pk, name: name))
  }
}
let result = IGListDiffable(oldPeople, people, .equality)
// do something w/ the diff

If people would find it handy, I'm more than happy to create a JSON example which takes a local json file parses it into objects and displays a mixed data view?

Feel free to assign this to me!

@rnystrom ok I think i got it now.
@Sherlouk I would be glad just to know that I'm doing it right 馃槃

馃挴 馃帀

Was this page helpful?
0 / 5 - 0 ratings