Questions tagged [nsmanagedobject]

NSManagedObject is a generic class that implements all the basic behavior required of a Core Data model object. NSManagedObject is available in OS X v10.4 and later and available in iOS 3.0 and later.

From the documentation:

Overview

A managed object is associated with an entity description (NSEntityDescription) that provides metadata about the object, including the name of the entity that the object represents and the names of its attributes and relationships. A managed object is also associated with a managed object context that tracks changes to the object graph.

[...]

Data Storage

In some respects, an NSManagedObject acts like a dictionary—it’s a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance. NSManagedObject supports a range of common types for attribute values, including string, date, and number (see NSAttributeDescription for full details).

Resource

Sample NSManagedObject declaration

In Objective-C:

NSManagedObjectContext *context = [self managedObjectContext];

//create a new managed object
NSManagedObject *newFlight = [NSEntityDescription insertNewObjectForEntityForName:@"FlightEvent" 
        inManagedObjectContext:context];

In Swift:

let newFlight = NSEntityDescription.insertNewObjectForEntityForName("FlightEvent", inManagedObjectContext: managedObjectContext)
1597 questions
24
votes
5 answers

An NSManagedObjectContext cannot delete objects in other contexts

I have two entities, each displaying on its own UITableView section. I've enabled editing to allow the user to delete rows by swiping to the right. This works fine for the first entity, but when I try to delete an object in the second entity, I get…
cetcet
  • 2,147
  • 2
  • 15
  • 15
24
votes
5 answers

Detecting changes to a specific attribute of NSManagedObject

How can I detect changes to a specific attribute of an NSManagedObject? In my Core Data data model, I have a Product entity that represents a product for sale. The Product entity has several attributes: price, sku, weight, numberInStock, etc.…
James Huddleston
  • 8,410
  • 5
  • 34
  • 39
23
votes
1 answer

Core Data merge two Managed Object Context

My Cocoa/Application has a Managed Object Context on the main thread. When I need to update my data my program will: Start a new thread Receive new data from a server Create a new Managed Object Context Send a notification to the main thread in…
23
votes
8 answers

Removing a specific entry/row from Core-Data

I'm using core data in my app, and i'm confused when it comes to removing certain rows or entries from the core data storage. I insert some products in to the storage like so: NSManagedObject *Product = [NSEntityDescription…
John S
  • 231
  • 1
  • 2
  • 3
22
votes
5 answers

Core-Data willSave: method

I have an attribute modificationDate in my Entity A. I want to set its value whenever NSManagedObject is saved. However, if i try to do that in NSManagedObject willSave: method, i get an error: *** Terminating app due to uncaught exception…
Mustafa
  • 20,504
  • 42
  • 146
  • 209
22
votes
3 answers

How can I create instances of managed object subclasses in a NSManagedObject Swift extension?

When creating an extension helper to NSManagedObject to create a new managed object subclass, swift provides the Self type to mimic instancetype which is great, but i can't seem to typecast from AnyObject. The below code does not compile with error…
amleszk
  • 6,192
  • 5
  • 38
  • 43
21
votes
3 answers

Comparing two NSManagedObjects

I have some code that loops through an array of NSManagedObjects and stops when it finds a certain record that is stored in an instance variable. The only way I can manage to see if they are the same record (not an equivalent record, the specific…
Nick Locking
  • 2,147
  • 2
  • 26
  • 42
21
votes
2 answers

NSManagedObject and KVO vs Documentation

I have a custom NSManagedObject subclass, say, Person. I also have a UIView registered with -addObserver:forKeyPath:options:context: to observe various properties of a Person, some of which are persistent like "name" and others are just dumb…
Costique
  • 23,712
  • 4
  • 76
  • 79
20
votes
6 answers

NSManagedObjectModel initWithContentsOfURL returns nil eventhough the modelURL is valid

my NSManagedObjectModel is returning nil eventhough the path is correct. NSString *modelKey = [NSString stringWithFormat:@"/%@/Model", name]; NSString *modelPath = [((Configuration *)[Configuration shared]) stringEntry:modelKey]; …
newDeveloper
  • 1,365
  • 1
  • 17
  • 27
20
votes
2 answers

With CoreData, if I have an @dynamic property, can I override its getter just like it was @synthesized? (Lazy Instantiation)

Using CoreData I created an entity, then I subclassed it into its own file, where it has the @propertys, then it has the @dynamic parts in the .m file. When I want something to have a certain value if it's never been set, I always use lazy…
user212541
  • 1,878
  • 1
  • 21
  • 30
19
votes
8 answers

CoreData issue: -[NSManagedObject setValue:]: unrecognized selector sent to instance

I just started with CoreData yesterday, and I'm going crazy :( I created a project that uses CoreData (ticked the box -use CoreData). Created the entities, and then created the NSManagedObject classes for all the entities (I suppose they create the…
Olsi
  • 929
  • 2
  • 12
  • 26
19
votes
3 answers

Subclassing NSManagedObject with swift 3 and Xcode 8 beta

I've began to try use Core data with swift 3 and Xcode 8 beta. When I try to generate NSManagedObject subclasses from core data model and Create NSManagedObject subclass… option in Editor menu, Xcode 8 beta generates three files one of them is…
RFG
  • 2,880
  • 3
  • 28
  • 44
19
votes
2 answers

CoreData. What's the difference between indexes and indexed?

I'm looking to speed up queries to my SQL backed CoreData instance (displaying records sorted by date). I know that indexing can help decrease query time, but what's the difference between: Highlighting the entity that an attribute belongs to, then…
VaporwareWolf
  • 10,143
  • 10
  • 54
  • 80
19
votes
1 answer

CoreData optional to-many relationships can never be nil?

Quirk I just discovered, and wanted to confirm with anyone here whether or not this is avoidable. Basically, if I have a very simple two entity model: With a to-many relationship between Entity1 and Entity2. The relationship is optional, with…
18
votes
7 answers

Get modification date for NSManagedObject in Core Data?

Outside of adding an NSDate property to each Entity in my Core Data store, is there a programmatic way to get the modification date for any object?
Jason
  • 14,517
  • 25
  • 92
  • 153