Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.85k stars 1.54k forks source link

How to deal with JSON data? #97

Closed yusuftor closed 7 years ago

yusuftor commented 7 years ago

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!

jessesquires commented 7 years ago

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:
kwstasna commented 7 years ago

@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 😃

rnystrom commented 7 years ago

@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
Sherlouk commented 7 years ago

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!

kwstasna commented 7 years ago

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

jessesquires commented 7 years ago

💯 🎉