0

I'm interested in knowing if there's a "proper" way to handle JSON responses from API requests.

Right now I have a struct that contains a static var. In the AppDelegate I grab the JSON from a URL (using Alamofire). I am able to reference it later in different views, however I have read that there are better ways than a "struct hack" to handle this.

My goal is to minimize the amount of calls, and only refresh when it's appropriate (ie. user "pulls down to refresh", "new object inserted into database", etc). Should this be how I handle it, or should I load different JSON responses on each new view, depending on what's required?

Any direction is appreciated.


Edit: Here's where I read that Structs were a "hack" for a "global variable". Maybe storing JSON in a struct is different than keeping a typical user variable in one. I don't know. See the second Answer's remarks.

Global Variables in Swift

As for the Struct I have:

In my jsonDataHolder.swift file:

struct jsonDataHolder {
    static var jsonData:AnyObject = []
}

In my AppDelegate.swift file, I have the following in DidFinishLaunchingWithOptions section:

Alamofire.request(.GET, "http://xxxxxxxxxx.com/yyy", encoding: .JSON).responseJSON { (_, _, JSONData, _) in
    jsonDataHolder.jsonData = JSONData!
}

Again, this all works fine. Just not sure if it's the "right" (if there is such a thing) way to do it.

Community
  • 1
  • 1
matcartmill
  • 1,573
  • 2
  • 19
  • 38
  • 1
    Edited my original question to give a bit more detail. – matcartmill Jan 10 '15 at 19:20
  • 1
    If you're going to down vote my question, at least leave a comment as to why. – matcartmill Jan 11 '15 at 04:09
  • I share your frustration with anonymous down-votes. (People should have the courage of their convictions and explain their down-vote so the OP learns how to improve questions in the future.) Anyway, I suspect this was down-voted because this topic has been covered exhaustively elsewhere. Search for "why are singletons evil" (or "why are globals evil") or "alternative to singleton". – Rob Jan 11 '15 at 14:50
  • 1
    It's hard sometimes to ask the right questions because being newer to iOS development (I have extensive web dev experience) I'm not always sure what certain things are called. For instance, a "Singleton" is new to me. Thanks for your help though, Rob. Greatly appreciated. – matcartmill Jan 11 '15 at 15:03

1 Answers1

0

If I understand the question correctly, though, this seems like the oft debated "singletons are evil" discussion. As we are supposed to avoid debating opinions here on Stack Overflow, I'll simply refer you to What is so bad about singletons?.

I must confess, though, that what I find more troubling is the the lack of any clear interface for this JSON structure. I'd rather see a model object with some logical public interface.

With this current object, your entire app (or at least everything that interfaces with this object) is dependent upon the private implementation details of the JSON.


The advice to avoid singleton-style patterns should not be accepted as dogma, but you should consider the reasons why that advice is generally offered (complicates testing, results in inappropriately tightly-coupled code, etc.). If you're adopting pattern like AnyObject that obscures implementation-dependent JSON structure, then there's no point in worrying about the singleton pattern, because you've already baked all of these limitations/problems in your code already.

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thanks for the response. One section of the app will have a "News Feed" type of functionality. Given that that type of data doesn't have a very long lifespan, I just figured it would be simpler to reference the JSON directly. It's really hard to discuss what I'm trying to do without giving away the details of the app. If I do a new API call on each page, this issue goes away, pretty much, but I'd like to try and limit the amount of calls, if possible. – matcartmill Jan 10 '15 at 22:41
  • Programming is constantly a weighing of these sorts of pragmatic considerations, so if you've decided that this is your approach, that's your call. But if you've made this particular decision, IMHO it makes the flaws of the `struct`-with-`static` pattern somewhat academic. – Rob Jan 11 '15 at 14:43