Questions tagged [core-data]

Core Data is Apple's object modeling and persistence framework for iOS, iPadOS, tvOS, watchOS, and macOS. Xcode provides an object model editor for specifying entities, attributes, and relationships.

Core Data is Apple's object modeling and persistence framework for macOS (), iPadOS (), tvOS (), watchOS () and iOS ().

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

While offering some functionality that one might traditionally associate with a relational database, Core Data, itself, is not a relational database management system. It is an object-oriented framework for managing, storing and retrieving model objects of a MVC (model-view-controller) design. However, in recent years Core Data got some SQL'ish features like having aggregates or subqueries on fetch request. But the core concept is still object-orientated, i. e. relationships are resolved by simply accessing properties instead of queries containing a primary key as a condition.

Core Data provides the developer with a wide array of features to simplify the management of these model objects. Instead of using file management and the arbitrary requests for the given store type, you interact with Objective-C and Swift objects.

The typical file Core Data stores its data into is a SQLite file. Although Core Data supports SQLite as one of its persistent store types, Core Data cannot manage any arbitrary SQLite database. In order to use a SQLite database, Core Data must create and manage the database itself. There is also a binary store type and a memory store type.

Core Data is part of the following SDKs:

  • iOS 3.0 and later,
  • macOS 10.4 and later,
  • Mac Catalyst 13.0 and later,
  • tvOS 9.0 and later,
  • watchOS 2.0 and later.

References

27960 questions
71
votes
6 answers

Custom setter methods in Core-Data

I need to write a custom setter method for a field (we'll call it foo) in my subclass of NSManagedObject. foo is defined in the data model and Xcode has autogenerated @property and @dynamic fields in the .h and .m files respectively. If I write my…
Andrew Ebling
  • 9,531
  • 7
  • 51
  • 72
71
votes
11 answers

Any way to pre populate core data?

I've been creating a list app and backing it with core data. I would like to have a default list of say 10 airport's items, so that the user doesn't have to start from scratch. Is there any way to do this? Any help is appreciated. Thanks in…
Tanner
  • 793
  • 1
  • 6
  • 12
71
votes
7 answers

NSFetchedResultsController with sections created by first letter of a string

Learning Core Data on the iPhone. There seem to be few examples on Core Data populating a table view with sections. The CoreDataBooks example uses sections, but they're generated from full strings within the model. I want to organize the Core Data…
Greg Combs
  • 4,222
  • 3
  • 30
  • 47
68
votes
5 answers

NSPredicate case-insensitive matching on to-many relationship

I am implementing a search field where the user can type in a string to filter the items displayed in a view. Each object being displayed has a keywords to-many relationship, and I would like to be able to filter the objects based on their…
Brian Webster
  • 11,623
  • 4
  • 41
  • 56
68
votes
10 answers

How to implement re-ordering of CoreData records?

I am using CoreData for my iPhone app, but CoreData doesn't provide an automatic way of allowing you to reorder the records. I thought of using another column to store the order info, but using contiguous numbers for ordering index has a problem. …
Boon
  • 37,606
  • 51
  • 186
  • 296
67
votes
2 answers

What is NSManagedObjectContext's performBlock: used for?

In iOS 5, NSManagedObjectContext has a couple of new methods, performBlock: and performBlockAndWait:. What are these methods actually used for? What do they replace in older versions? What kind of blocks are supposed to be passed to them? How do I…
nevan king
  • 108,735
  • 42
  • 196
  • 237
67
votes
15 answers

Xcode - error: pathspec '...' did not match any file(s) known to git

I am using a local git repo. When I try to commit changes to the core data model file (.xcdatamodel), I get this message: error: pathspec '.../DataModel.xcdatamodeld/DataModel.xcdatamodel/contents' did not match any file(s) known to git. How to fix…
Abdalrahman Shatou
  • 4,285
  • 6
  • 45
  • 72
67
votes
6 answers

Where to place the "Core Data Stack" in a Cocoa/Cocoa Touch application

In the iPhone Core Data Template, Apple places the Core Data Stack in the App Delegate. My initial inclination however is to move this code into it's own class whose responsibility is to handle the management of the Core Data Stack. Do you typically…
Corey Floyd
  • 25,769
  • 30
  • 123
  • 152
66
votes
1 answer

Core Data and iOS 7: Different behavior of persistent store

I'm preparing an update for a Core Data based app for fixes with iOS 7. I use Xcode 5 and iOS 7 SDK GM. However I realized a different behavior of the persistent store (which is a UIManagedDocument): Prior to iOS 7 builds there was only one file…
FrankZp
  • 1,996
  • 3
  • 21
  • 36
65
votes
3 answers

Core Data: NSPredicate for many-to-many relationship. ("to-many key not allowed here")

I have two entities named "Category" and "Article" which have a many to many relationship. I want to form a predicate which searches for all articles where category.name is equal to some value. I have the following: NSEntityDescription …
Oh Danny Boy
  • 4,692
  • 8
  • 54
  • 85
65
votes
24 answers

iOS: Delete ALL Core Data Swift

I am a little confused as to how to delete all core data in swift. I have created a button with an IBAction linked. On the click of the button I have the following: let appDel: foodforteethAppDelegate = UIApplication.sharedApplication().delegate as…
Prateek
  • 1,634
  • 3
  • 13
  • 27
65
votes
3 answers

Core Data multi thread application

I'm trying to use core data in a multi thread way. I simply want to show the application with the previously downloaded data while downloading new data in background. This should let the user access the application during update process. I have a…
SlowTree
  • 1,235
  • 1
  • 10
  • 18
64
votes
22 answers

Xcode 8 generates broken NSManagedObject subclasses for iOS 10

I updated my iOS app project recently to iOS 10. Now I'm trying to change the Core Data Model of my app but the new NSManagedObject subclasses which Xcode generates are broken. I also tried to fix the subclasses manual but this doesn't work. The…
Remco Beugels
  • 1,075
  • 1
  • 11
  • 21
64
votes
9 answers

iPhone Core Data "Automatic Lightweight Migration"

I am attempting to update an app that implements a core data store. I am adding an attribute to one of the entities. I added the following code to my delegate class: - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if…
Eric
  • 1,329
  • 1
  • 13
  • 19
63
votes
9 answers

Creating a unique id for a Core Data program on the iPhone

I am having a bit of trouble figuring out this Core Data stuff. How do I create a new entry with a unique ID? In SQL I would just declare one field as an autoincrement field. I'm not seeing anything like that here, but I could just be missing…
user165411