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.