0

Is there anything different between the two? I'm trying to figure out which one is better to use because they seem to do the same thing, or at least to me. I'm trying to pass data between multiple views.

4 Answers4

4

Core Data is an object database that usually has an SQL relational database as it's backing store. It is used for storing and manipulating complex data-sets, and presenting them to the user.

PrepareForSegue is a method that gets called on a view controller before a new view controller is invoked from a segue. You can use prepareForSegue() as a way to pass data to the new view controller that is about to be displayed.

The two things have very little in common.

Since Core Data is a way to store app-wide persistent data, you can have multiple view controllers read and write data to a shared Core Data database, and in that way communicate information between view controllers, but that's about as far as the comparison goes.

To use an analogy, Core Data is a filing cabinet that everybody in an office has access to. If a clerk files some information in the filing cabinet, at any future time another employee can find that file and get the information, and so can anyone else in the company who has access to the cabinet. PrepareForSegue is a text message between 2 specific phone numbers. It transmits a burst of transient information, once and only once, between specific people.

EDIT:

There are other options for passing information between view controllers as well.

Check out this question/answer thread I created on the subject:

How do you share data between view controllers and other objects in Swift?

Community
  • 1
  • 1
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks. This response really clears things up for me. I'm passing data between multiple view controllers and looks like I should be using Core Data instead of PrepareForSegue. – Cornelius N. Dec 14 '16 at 19:04
  • Core data is an advanced framework with a steep learning curve. If you need to pass complex, structured data, or do analysis/sorting of that data, it's a good choice, but unless you need those abilities, it's likely overkill. You should explain what you're trying to do. – Duncan C Dec 14 '16 at 19:09
  • I have four viewControllers that have a UIImageView and TextView. I need to take all their data and pass it to another ViewController that'll display all of it in one place. – Cornelius N. Dec 14 '16 at 19:19
  • You should use something simpler like NSUserDefaults (which would let your data persist between runs) or a data container singleton (Which would give you a globally available memory based model object.) You're going to strain your brain trying to learn Core Data and it's overkill for your needs. – Duncan C Dec 14 '16 at 19:26
1

If you want the data to persist then you must use SQLite3, Core Data or something like realm.

If you just want to pass the data around from one viewController to another then you would do it through something like prepareForSegue

mfaani
  • 33,269
  • 19
  • 164
  • 293
0

PrepareForSegue would be to pass (tempory) data between two viewControllers. CoreData is a library to allow you to store data inside your app and re-use it whenever you want wherever you want.

robin.s
  • 104
  • 1
  • 1
  • 9
  • Okay, the difference is temporary and permanent. So with PrepareForSegue could you pass data between three or more viewControllers? Or just the two viewControllers? – Cornelius N. Dec 14 '16 at 19:00
  • @CorneliusN. You can pass your data as much as you want. Just remember that because it's temporary if you your controller gets deinit, it will loose its data. (if you close the app or pop the viewController) – robin.s Dec 14 '16 at 20:41
0

PrepareForSegue allows you to get a reference to the destination view so you can pass the variables you want.

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    let viewController = segue!.destinationViewController as ViewController
    //here you can access view variables as viewController.variable
}

CoreData instead allows you to persist data to a SQL Database, maybe not what you want.

But beware, you can only pass data from de origin view to the destination one.