Servicio web iOS: mover la llamada de conexión desde AppDelegate

So I've been working on fetching data from a web service and using it in my app (e.g displaying a bunch of names in a UITableView)...

The guide I have been following to setting this up, set up the NSURLConnection in the AppDelegate, then had a XMLParser and a object for storing the fetched data... Now, I want to move the connection set-up out and call from the AppDelegate and into a class of it's own... But I'm a bit unsure about the approach...

I'm thinking something along the line of:

Child (object for storing data)

ChildParser (NSXMLParser)

SomeViewController (receives update notification from the parser and uses the data)

ChildMsg (set up the connection and SOAP message here)

Okay, so before the AppDelegate set up the connection and SOAP message in didFinishLaunchingWithOptions... and in connectionDidFinishLoading it initialized the ChildParser... then in the viewController it initiliazed the parser with:

appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

and I could then access the Child data objects...

Okay, so what I'm thinking is that I should not be setting up the connection in the AppDelegate as I'm going to have 3-4 different connections and a bunch of SOAP messages for each connection, so it would quickly make a mess of the AppDelegate class... Therefore, I'm thinking that I'll create a class for each of the different web service calls and then initialize these classes in a viewController when I need them... But how would I do this?

Before I had this in my parser:

- (XMLParser *) initXMLParser {

    self = [super init];

        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    return self;

which I used to initialize it in the AppDelegate, so I'm guessing I'll need something similar in ChildMsg

This rambling probably doesn't make sense, but hopefully you understood what I want to achieve and can give me a nudge in the right direction...

Short summary: I'm parsing data for a web service, and I want to get the data in a viewController, but I want to set up the connection and SOAP message another place than the AppDelegate..

Gracias por adelantado

preguntado el 02 de febrero de 12 a las 10:02

1 Respuestas

I would suggest trying to follow the MVC (Model View Controller) approach here. I think that you have pretty clear what your view and controller classes are; the model is where you could put your SOAP/XML code (or in a class that you access from the model).

In this case, add an ivar to your controller to represent your model class. The model class should offer methods to update its content (i.e., get data from the web service) and to retrieve single pieces of data that your controller makes then available to the view it manages. The model class would then encapsulate your connection/request handling code, and (possibly, if I understand things correctly) also access ChildParser to build Child objects (that the view would use to display its content).

This is just a rough sketch of how things would work. If you need more information about MVC, you could have a look to Wikipedia . Espero que esto ayude.

Respondido 02 Feb 12, 14:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.