Barkley
Barkley

Reputation: 6713

Maintaining data in arrays after leaving that view?

I'm adding content to a collection view on my main view controller, adding that content to an array, navigating through my app, coming back and the collection view is completely empty.

I find it strange that I haven't run into this issue before, because I've gone through tutorials and made todo lists and similar apps where you go to another page, come back, and the data is still in tact.

So if arrays lose their data when you leave the VC how do you keep the collection view okay after you've left? reloading data wouldn't work because coming back to the screen the array of content is empty.

Upvotes: 0

Views: 1310

Answers (2)

GJZ
GJZ

Reputation: 2542

The problem you have is that you do not have persistent storage. In iOS, if something is not stored on the device, it can be lost easily once you leave the View Controller.

//////////////////////////////////////////////////////////////////////

EDIT:

Variables lose their values when the object that holds them is deallocated, or when their value is explicitly altered one way or another (see other answer).

Certain segue types, for example, can create a new instance of a VC, resulting in a loss of data. Storing a value permanently allows one to bypass these issues.

/////////////////////////////////////////////////////////////////////

In terms of persistent storage, there are several options:

1) Using an online database (though WiFi is required)

2) Using NSUserDefaults (easy persistent storage for fairly small information: avoid images)

3) Core Data (on-device persistent storage: more complicated but much more powerful)

I would recommend beginning with NSUserDefaults for arrays, since this is easy to learn.

Here is how you can write to NSUserDefaults:

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(myArray, forKey: "arrayKey")

And this is how you can retrieve the data:

var retrievedArray = defaults.objectForKey("arrayKey") as [String]

I am assuming it is an array of strings. So when you add data to the array, store it in NSUserDefaults, then, when you need to retrieve the data, take it from there.

In addition, this will allow you to access this information in other ViewControllers as well as after an app has been terminated. In other words, the data is there to stay. You can still overwrite it of course, as well as remove it by deleting the app.

Take a look at this: https://www.hackingwithswift.com/read/12/2/reading-and-writing-basics-nsuserdefaults

Upvotes: 2

David Ganster
David Ganster

Reputation: 1993

So if arrays lose their data when you leave the VC

They don't. They lose their values when the object that holds them is deallocated, or when they are explicitly cleared/re-initialized/deleted.

Are you sure the view controller that holds the collection view isn't deallocated? Are you resetting the arrays in viewWillAppear(), viewDidAppear(), viewWillDisappear() or viewDidDisappear()?

Upvotes: 2

Related Questions