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
246
votes
33 answers

Delete/Reset all entries in Core Data?

Do you know of any way to delete all of the entries stored in Core Data? My schema should stay the same; I just want to reset it to blank. Edit I'm looking to do this programmatically so that a user can essentially hit a reset button.
Michael Grinich
  • 4,770
  • 8
  • 29
  • 30
138
votes
13 answers

Unable to find specific subclass of NSManagedObject

I'm working on developing an app with Core Data. When I created an instance using: let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: appDelegate.managedObjectContext) let user = User(entity: entity,…
MsrButterfly
  • 1,599
  • 2
  • 10
  • 17
97
votes
13 answers

CoreData: warning: Unable to load class named

I am duplicating an existing Objective-C TV Show app to a new Swift version using Xcode 6.1 and am having some issues with CoreData. I have created a model of 4 entities, created their NSManagedObject subclass (in Swift), and all files have the…
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
80
votes
4 answers

@property definitions with ARC: strong or retain?

Using Xcode 4.2 and ARC, I notice that the auto-generated code for an NSManagedObject still reads like this for properties: @property (nonatomic, retain) NSString * someString; 1) Shouldn't retain now be replace with strong or weak? 2) Why does the…
75
votes
12 answers

Multiple NSEntityDescriptions Claim NSManagedObject Subclass

I am creating a framework that allows me to use Core Data. In the framework's test target, I have configured a data model named MockModel.xcdatamodeld. It contains a single entity named MockManaged that has a single Date property. So that I can test…
Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49
73
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,410
  • 5
  • 34
  • 39
66
votes
17 answers

How can I duplicate, or copy a Core Data Managed Object?

I have a managed object ("A") that contains various attributes and types of relationships, and its relationships also have their own attributes & relationships. What I would like to do is to "copy" or "duplicate" the entire object graph rooted at…
Mani
  • 714
  • 1
  • 6
  • 8
65
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,153
  • 1
  • 12
  • 21
63
votes
2 answers

Invalid redeclaration on CoreData classes

I am working with CoreData, on an entity called "RoleName". The problem is: I click on "Create NSManagedObject subclass" from within my model, and so it automatically creates the classes for my entity. However, on the declaration of the class, I get…
dpstart
  • 1,018
  • 1
  • 10
  • 25
60
votes
6 answers

Is there a way to instantiate a NSManagedObject without inserting it?

I have a user interface to insert a Transaction. once the user clicks on a plus he gets the screen and i want to instantiate my Core Data NSManagedObject entity let the user work on it. Then when the user clicks on the Save button i will call the…
58
votes
3 answers

How can I track/observe all changes within a subgraph?

I have a NSManagedObjectContext in which I have a number of subclasses of NSManagedObjects such that some are containers for others. What I'd like to do is watch a top-level object to be notified of any changes to any of its properties,…
David Carney
  • 2,200
  • 1
  • 19
  • 28
57
votes
5 answers

How do I copy or move an NSManagedObject from one context to another?

I have what I assume is a fairly standard setup, with one scratchpad MOC which is never saved (containing a bunch of objects downloaded from the web) and another permanent MOC which persists objects. When the user selects an object from scratchMOC…
55
votes
2 answers

Swift + CoreData: Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

I have a coredata entity named Record and has a property dateUpdated. I noticed that the generated NSManagedObject subclass has no optional mark (?) CoreData Editor: Generated Subclass: Expected: UPDATED: It's tedious in my part, because each…
Allan Macatingrao
  • 2,071
  • 1
  • 20
  • 28
53
votes
4 answers

Core Data primary key ID for a row in the database

Suppose I have a list of books stored in Core Data. I want to search for a book by it's primary key ID. I know the sqlite file created by Core Data has an ID column in each table, but this doesn't seem to be exposed to me in anyway. Does anyone have…
WoodenKitty
  • 6,521
  • 8
  • 53
  • 73
53
votes
8 answers

invalid redeclaration in auto code generate NSManagedObject Subclass Swift 3

Using Version 8.1 of Xcode. Create an entity named "MapRegionObject" in .xcdatamodeld file. Using auto code generator, click Editor on the navigation bar -> create NSManagedOject Subclass... Got two files : MapRegionObject+CoreDataClass.swift and…
icelemon
  • 827
  • 2
  • 11
  • 23
1
2 3
99 100