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
96
votes
10 answers

What do I have to do to get Core Data to automatically migrate models?

I have read the documentation about automatic /lightweight migration for Core Data models - but I am having problems implementing it. As I understand it the application should notice that the model it has and the model that exists on a device…
Grouchal
  • 9,683
  • 6
  • 30
  • 45
93
votes
8 answers

JSON and Core Data on the iPhone

I have a core data object graph (consisting of two entities linked by a to-many relationship). I was curious, as a relatively inexperienced iPhone developer, whether anyone could recommend an approach, and a suitable JSON implementation for the…
Urizen
  • 2,213
  • 6
  • 23
  • 32
90
votes
6 answers

How to write a BOOL predicate in Core Data?

I have an attribute of type BOOL and I want to perform a search for all managed objects where this attribute is YES. For string attributes it is straightforward. I create a predicate like this: NSPredicate *predicate = [NSPredicate…
Proud Member
  • 38,700
  • 43
  • 143
  • 225
86
votes
8 answers

How to Deal with Temporary NSManagedObject instances?

I need to create NSManagedObject instances, do some stuff with them and then trash them or store to sqlite db. The problem is, I cannot create instances of NSManagedObject unconnected to NSManagedObjectContext and this means I have to clear up…
fspirit
  • 2,437
  • 2
  • 18
  • 26
86
votes
5 answers

iPhone Core Data "Production" Error Handling

I've seen in the example code supplied by Apple references to how you should handle Core Data errors. I.e: NSError *error = nil; if (![context save:&error]) { /* Replace this implementation with code to handle the error appropriately. abort()…
Sway
  • 1,587
  • 3
  • 15
  • 19
85
votes
3 answers

Example or explanation of Core Data Migration with multiple passes?

My iPhone app needs to migrate its core data store, and some of the databases are quite large. Apple's documentation suggests using "multiple passes" to migrate data to reduce memory use. However, the documentation is very limited and doesn't…
Jason
  • 14,035
  • 24
  • 87
  • 151
85
votes
4 answers

How to sync CoreData and a REST web service asynchronously and the same time properly propagate any REST errors into the UI

Hey, I'm working on the model layer for our app here. Some of the requirements are like this: It should work on iPhone OS 3.0+. The source of our data is a RESTful Rails application. We should cache the data locally using Core Data. The client code…
eploko
  • 5,328
  • 2
  • 23
  • 23
84
votes
3 answers

Core Data background context best practice

I have a large import task I need to do with core data. Let say my core data model look like this: Car ---- identifier type I fetch a list of car info JSON from my server and then I want to sync it with my core data Car object, meaning: If its a…
Eyal
  • 10,429
  • 18
  • 73
  • 127
81
votes
7 answers

Core Data Primary Key

This may seem stupid, but I still couldn't figure out how to mark a attribute as a primary key in the xcdatamodel file. My persistent storage is sqlite file. Can anyone help me? In that case, how can I "validate" a ID to be unique? Should I write a…
Mugunth
  • 14,355
  • 15
  • 64
  • 94
78
votes
1 answer

CoreData + iCloud + Cascade Delete - how to handle?

CoreData Entity "A" has a one-to-many relationship to a collection of CoreData Entries "B", using a Cascade delete rule. In an iCloud environment, while device 1 shows a detail view of one of the "B" entries, device 2 deletes the "A" entry. When the…
Amiram Stark
  • 2,178
  • 20
  • 30
77
votes
5 answers

Core Data - Failed to load optimized model at path

I'm getting some of these prints in my console while running my application from Xcode 6 in my iPhone 6 with iOS 9 beta 5: CoreData: Failed to load optimized model at path…
Glauco Neves
  • 3,345
  • 1
  • 21
  • 36
75
votes
7 answers

How to store an image in core data

I'm new to iOS. I've been trying to make an application that will store an image captured from the camera into CoreData. I now know how to store data like NSStrings, NSDate and other type but struggling to store an image. I've read so many articles…
Sipho Koza
  • 862
  • 1
  • 9
  • 20
73
votes
2 answers

What NSNumber (Integer 16, 32, 64) in Core Data should I use to keep NSUInteger

I want to keep NSUInteger into my core data and I don't know which type should I use (integer 16, 32, 64) to suit the space needed. From my understanding: Integer 16 can have minimum value of -32,768 to 32,767 Integer 32 can have minimum value of…
sarunw
  • 7,448
  • 10
  • 43
  • 76
73
votes
8 answers

'+entityForName: nil is not a legal NSManagedObjectContext parameter - Core Data

I have added all of the relevant code to the App Delegate, and I am able to add to the data model and fetch from the data model in applicationDidFinishLaunchingWithOptions. My problem comes when I am trying to write to the data model in my View…
Alex Godbehere
  • 2,862
  • 6
  • 28
  • 52
71
votes
5 answers

How can I tell whether an `NSManagedObject` has been deleted?

I have an NSManagedObject that has been deleted, and the context containing that managed object has been saved. I understand that isDeleted returns YES if Core Data will ask the persistent store to delete the object during the next save operation.…
James Huddleston
  • 8,222
  • 4
  • 32
  • 39