Creating a single JSON Parser/Network object

大兔子大兔子 提交于 2020-01-25 01:02:40

问题


What I'm looking for is a point in the right direction of helping me create a better concept of what I need to do within my application design.

What I'm trying to do is have a class that will make network requests, IE: getUser, getTimeline, getLocation. Once the data has been recieived via -(void)connectionDidFinishLoading: have JSON (for my application I'm using TouchJSON) to parse the the data and with that data return an array or object of it.

All the above I can do, however I find I need to do it in every class I create which leads to a lot of code, and redundant code at that.

Basically if I want to do this approach now this is a rough walkthrough of what happens:

So for example:

  1. [View 1]->NetworkActivity Object -> getUser
  2. [NetworkActivity] -> getUser -> makeConnection -> parseObject -> return object
  3. [View 1]->getUser method is now complete and is empty, which in turn now means I have nothing to show

Now I understand that because im using Asynchronous request this is why this is happening, however what I'm after is a way to accomplish this approach without having to create several classes that basically do the same thing. I'm not looking for "the answer" just a nudge or a point in the right direction of something I can read or try that would better help me understand this.

Also I'm sorry if this was at all confusing, I tried to describe it as best as I could, but please do understand - I'm kinda confused at it all myself!


回答1:


Take a look at Matt Gemmell's MGTwitterEngine which is a great example of how to develop a client that targets a specific API, Twitter. If you know that the serve implements RESTful web services, then you can also take a look at the iPhone on Rails project which implements a generic controller. You provide the model and the controller takes care of the REST.



来源:https://stackoverflow.com/questions/3162212/creating-a-single-json-parser-network-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!