Streaming JSON with AFNetworking on IOS

后端 未结 1 424
故里飘歌
故里飘歌 2021-01-02 22:14

What would be the most elegant way to receive data from a streaming JSON API using AFNetworking? AFNetworking provides excellent support for receiving non-streaming data fro

相关标签:
1条回答
  • 2021-01-02 22:41

    AFNetworking does not have a built-in streaming SAX-style JSON operation, but it should be straight forward to create your own.

    • Create a subclass of AFJSONRequestOperation
    • Use the outputStream property of the operation during initialization to hook it up to a JSON parser that supports SAX-style parsing (such as Yajl). The parser will read data and build up the JSON object as it comes in
    • Hook up the responseJSON property to read the cached object from the parser
    • ...or depending on how you're doing streaming, you may need to add a new property that defines a block to execute when new JSON objects come back (e.g. setReceivedJSONBlock:((^)(void (id JSON))block). This block will probably be triggered by delegate methods sent from the parser (e.g. <YAJLParserDelegate> -parserDidEndDictionary:).

    If you are able to get this working, I would encourage you to publish and share this with others. I think this could be useful to quite a few people.

    0 讨论(0)
提交回复
热议问题