0

I have the following situation:

enter image description here

I have a UIViewController that has embedded two Container Views, one is hidden and one is visible. Both of those Container Views have embedded another UIViewControllers with some elements.

I want to present a data fetched from webservice on both panels, but I want to fetch the data only once, then just parse it in a specific way, based on what panel user currently sees.

I have a method that fetches data as json:

func loadInitialDataWithoutCluster() {
        print("loadInitialData");
        RestApiManager.sharedInstance.getRequests { json in
            if let jsonData = json.array {
                for requestJSON in jsonData {
                    dispatch_async(dispatch_get_main_queue(),{
                        if let request = SingleRequest.fromJSON(requestJSON){

                          //how can I add those single requests
                          // to a list/array/whatever here that will be
                          // accessible from both of two other panels?
                        }
                    })
                }
            }
        }

    }

and then when it fetches all the data and (somehow) assigns it to the list/array - how can I refer to it from two different panels?

user3766930
  • 5,629
  • 10
  • 51
  • 104

2 Answers2

0

This would be the solution for best programming practice. Technically you shouldn't allow any class to manipulate your data directly, instead create functions which do the work for you. I have included a function which reads and writes to an array, you can manipulate these to your needs.

class ArrayObject {

  private var downloadedData = [1,2,3]

  internal func readData() -> [Int] {
    return downloadedData
  }

  internal func addData(number: Int) -> [Int] {

    downloadedData.append(number)

    return downloadedData
  }
}


class genericControllerClass: UIViewController {

  func currentData() {
    let getUsers = ArrayObject().addData(15)
  }
}
Ben Sullivan
  • 2,134
  • 1
  • 18
  • 32
-1

You can create a new file with a simple structure to store an array of the details you need, you can access this from anywhere in the project.

struct Arrays {

  static var downloadedData = [TheTypeYouNeedToStore]()
}

You can add to this array once the data has been downloaded just by using Arrays.downloadedData.append(). Then anywhere else in the project this can be accessed and modified using Arrays.downloadedData[0]

Ben Sullivan
  • 2,134
  • 1
  • 18
  • 32
  • Does it have to be static? I created a file Events.Swift and now when I type in some other place `Events.downloadedData.append...` I'm getting error `Instance member 'downloadedData' cannot be used on type Events` – user3766930 Feb 25 '16 at 19:16
  • Whoops yes, needs to be static! – Ben Sullivan Feb 25 '16 at 19:17
  • now my problem is that when I add any data to this array by doing it asynchronously: `dispatch_async(dispatch_get_main_queue(),{ if let request = SingleRequest.fromJSON(requestJSON){` then the array is always empty wherever I call it - it's a threading problem probably, I asked another question about it here: http://stackoverflow.com/q/35633199/3766930 Do you maybe know how to handle it? – user3766930 Feb 25 '16 at 19:29
  • Is your code on that up to date? I can't see that you've used the new struct on there. I'm not too great with threading issues but I'll take a look – Ben Sullivan Feb 25 '16 at 19:38
  • using globals to pass around data is a bad idea: it messes up memory management, introduces [shared mutable state](https://twitter.com/teozaurus/status/518071391959388160), violates the principles of [SOLID](https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)) – vikingosegundo Feb 25 '16 at 20:30
  • What would you recommend instead? I've been looking for a best practice alternative – Ben Sullivan Feb 25 '16 at 20:33
  • just passing data either as parameter for the constructor or properties. – vikingosegundo Feb 25 '16 at 20:34
  • Sorry can you clarify what that would practically look like perhaps in an answer or just on here. Would be acceptable to store the data in a class object instead of a struct? – Ben Sullivan Feb 25 '16 at 20:51
  • what do you mean by *class object* in this context? – vikingosegundo Feb 25 '16 at 23:36
  • I'm not a hugely experienced developer and it would be very helpful if you could post an answer to this question so we can all learn from it – Ben Sullivan Feb 26 '16 at 07:58
  • I've added a new 'best practice' answer and will remove this question once the other is accepted as the answer. – Ben Sullivan Feb 26 '16 at 17:23