Streaming JSON with AFNetworking on iOS

What will be the most elegant way to retrieve data from a streaming JSON API using AFNetworking? AFNetworking provides excellent support for receiving non-streaming data from the JSON API, but I could not find examples of streaming JSON.

+6
source share
1 answer

AFNetworking does not have a JSON-style SAX streaming built-in, but it must be direct in order to create its own.

  • Create a subclass of AFJSONRequestOperation
  • Use the outputStream property of the operation during initialization to connect it to a JSON parser that supports SAX-style parsing (for example, Yajl ). The analyzer will read the data and create a JSON object, since it is included in
  • Connect the responseJSON property to read the cached object from the analyzer
  • ... or depending on how you do streaming, you may need to add a new property that defines the block to execute when new JSON objects are returned (e.g. setReceivedJSONBlock:((^)(void (id JSON))block ) This block is likely to be called by delegation methods sent from the analyzer (for example, <YAJLParserDelegate> -parserDidEndDictionary: .

If you can achieve this, I would recommend that you publish and share this with others. I think this can be useful for many people.

+12
source

Source: https://habr.com/ru/post/908920/


All Articles