0

In an app that I am working for, I need array variable that can be used in all UIViews. Currently when the array is changed in a view it is stored in the database, and when I need the updated version of the array in the previous view, I use the viewWillAppear method and retrieve the updated array from the database. While going to another view by a segue, I use the passing data by prepareForSegue, but if I use the back button, or just change the screens via tab bar, I use the viewWillAppear and a query.

Is there any way that when the array is created in a view, the data in it will be accessible in all views?

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
saner
  • 821
  • 2
  • 10
  • 32
  • Of course you can use a singleton but that's generally frowned upon.(see link) I'll be interested to see what other options are presented given that you don't want to keep saving / fetching the record. (link: http://stackoverflow.com/questions/137975/what-is-so-bad-about-singletons) – Dan Beaulieu Dec 29 '15 at 19:06
  • please don't use singletons or any other global data. pass data from view controller to view controller. from there to the view. create a common UIView base class that knows how to handle your data. here I posted code to show how passing form vc to vc can be done easily by subclassing. subclasses views are not covered. http://stackoverflow.com/questions/32979924/passing-data-from-the-firstviewcontroller-to-the-lastviewcontroller/32980270#32980270 – vikingosegundo Dec 30 '15 at 02:49

2 Answers2

2

As I've stated in my comment, singletons are generally frowned upon for a myriad of reasons. However, there is much debate on this topic:

What is so bad about singletons?

Having said that, the best way I know to make a variable globally available for the session is by creating a singleton.

struct myArray {
    static var data: [Int] = []
}

You could set this singleton up to fetch the records using CoreData and store the current working version in a static variable for quick access.

note: I am really very curious to see some other approaches.

Community
  • 1
  • 1
Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
0

Singleton

Singleton is basically a global variable that you can use them in any views, but some developers experience some bugs and difficulties, use it at your own risk, I recommend this method when you're definite that you will use that data a lot (STILL RISKY), but this method is like goddess of data handling :).

Create a NSObject subclass and call it DataManager.swift (I call it data manager cause it handle data.) as following:

import UIKit

class DataManager: NSObject {
        
   //Store Data Globally
   static var someData: NSArray! //This Boolean, you can choose whatever you want.
}

the static is what keep your data live.

Now you can store and receive someData from anywhere like you handle any data type like this.

//Store
DataManager.someData = []

//Receive 
print(DataManager.someData)
Community
  • 1
  • 1
Brian Nezhad
  • 6,148
  • 9
  • 44
  • 69
  • Hello Farhad, which one would you offer more, the singleton or global variables that are defined just under the import part. What are the advantages and disadvatages of them? – saner Dec 30 '15 at 09:14
  • Well, you want to use the array in other views and be reliable, then you need singleton. A global variable is safe (not all the time though) cause is out of functions, but more limited. But I recommend singleton. Search for `struct` also, it might help. – Brian Nezhad Dec 30 '15 at 16:14