-2

just started iOS 7 development. I'm building an app which stores a user profile. Certain data, like photos and reviews, need is related to multiple users.

From researching, I found that Core Data essentially provides this type of relational DB. But super n00bie question - is a Core Data the same as a Singleton? Which should I use for accessing user data across multiple viewControllers?

If I go the Core Data route, it seems that I'll have instantiate a Core Data object in every ViewController. Doesn't that seem excessive??

With Singletons, I won't have to do that, but I'll need to have proper thread management.

Sound right?

Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235

2 Answers2

1

CoreData is an object model framework, and a singleton is a design pattern. This isn't even comparing apples to oranges, because apples and oranges are both fruits.

But your question makes it seem like you want to know how to access your data throughout your app. There are a few common patterns for this.

You typically use one or more instances of NSManagedObjectContext. You can pass this instance along to each view controller. Or you can have some singleton object which has a reference to the NSManagedObjectContext. This singleton is often the App Delegate, especially since the built in Xcode templates already come with a reference to your NSManagedObjectContext in the App Delegate.

Richard Venable
  • 8,310
  • 3
  • 49
  • 52
  • Yes! I want to understand the best way to access data throughout the app. Is there a preferred method to access data - Singleton vs. NSManagedObjectContext? Does one have a steeper learning curve than the other? – foodieNcoder May 03 '14 at 22:28
  • I don't think you are understanding - the phrase "Singleton vs. NSManagedObjectContext" does not make sense. Yes, you should learn about CoreData in the long run, but you can make some simple apps without it. – Richard Venable May 03 '14 at 22:31
  • Read this: http://en.wikipedia.org/wiki/Singleton_pattern and this: http://en.wikipedia.org/wiki/Core_Data to help with some of your confusion – Richard Venable May 03 '14 at 22:32
  • I think Aaron's comment below explains it well. I can use NSManagerdObjectContext in each view controller OR use a Singleton to access Core Data. I get it - they're used together, it's not one or the other. But there are 2 ways to attack the problem. – foodieNcoder May 03 '14 at 22:36
  • 1
    Yeah, thats right. Good job! If you are first starting with CoreData, I recommend creating a new project with Xcode and make sure you check the 'Use Core Data' checkbox. When your project is created, you can look at your App Delegate object and it will have your basic Core Data stack already set up for you. It will have the instance of NSManagedObjectContext that you need, and you can then decide how you want to get that to your other view controllers. – Richard Venable May 03 '14 at 22:41
0

it's not the same thing at all.

Core Data allows you to create subclass of NSMamagedObject which are object representing table in a sql data base stored on disk.

singleton are unique instance of any class (usually if not never an NSManagedObject).

read on wikipedia about singleton it's a good article.

both have nothing to do with iOS7

Nicolas Manzini
  • 8,379
  • 6
  • 63
  • 81
  • Thanks for the quick response. Link please? – foodieNcoder May 03 '14 at 22:20
  • 2
    seriously? cannot type singleton in wikipedia search box? – Nicolas Manzini May 03 '14 at 22:21
  • Lots can be lost in translation :). Seriously? You respond to complain, but not just answer the question?? Def on a developer forum. – foodieNcoder May 03 '14 at 22:25
  • To expand: you might *use* a singleton class to access Core Data (or any other data model), so that you don't need to pass it between view controllers or create Managed Object Contexts in each view controller. They are not "the same", but it may be common to see them together. – Aaron Brager May 03 '14 at 22:28
  • i dont know the link i would have to search for you here i can just read you and type – Nicolas Manzini May 03 '14 at 22:29
  • http://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – Aaron Brager May 03 '14 at 22:30
  • Ah, great explanation; thanks, Aaron!! I think there's a steeper learning curve with singletons, although probably reduce the amount of code in each viewController. – foodieNcoder May 03 '14 at 22:32