mercredi 15 juillet 2015

Design patterns used to handle a series of interdependent network requests

I am new to iOS development and I am working on a weather app that makes a series of requests that all rely on the previous and can all potentially fail. Basically, when the user opens the app or refreshes it, I want the following to happen:

  1. Check that the user has authorized the app to use their location and that they have enabled location services on their device.

  2. Use their location to make a call to a weather service.

  3. Use the data returned by the weather service to make an http request to another service to grab related data.

  4. Use the data I get back to refresh the UI.

I only want to update the UI when I have ALL the data I need, even though I could update it bit by bit as things are returned to me. Furthermore, if any step fails, I want to stop the update entirely and notify the user.

As I've been trying to implement this, I've been having a hard time keeping it clean. My code is jumping all over the place (from ViewController to my CLLocationManagerDelegate back to my ViewController then to another class, etc.) and I'm passing around a lot of closures which is becoming confusing to follow.

I can get it all to work, but when I come back to make updates in the future, I don't want to be confused looking at my code. Are there any common design patterns/principles out there that are used for this sort of situation? Any suggestions to make this clean would be much appreciated!

Aucun commentaire:

Enregistrer un commentaire