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
12
votes
3 answers

Unacceptable type of value for to-one relationship: property = "user"; desired type = User; given type = User;

I'm having a wired problem with core data. With Swift3 in iOS 10 I get the managed object context each time I am fetching or storing data with func getContext () -> NSManagedObjectContext { let appDelegate = UIApplication.shared.delegate as!…
12
votes
3 answers

How to convert NSManagedObject to NSDictionary

I am trying to convert NSManagedObject to NSDictionary this is what I tried: var keys:NSArray = order?.entity.attributesByName.keys var dict:NSDictionary = order?.dictionaryWithValuesForKeys(keys) But I get error: …
1110
  • 7,829
  • 55
  • 176
  • 334
12
votes
1 answer

Adding relationships in NSManagedObjectModel to programmatically created NSEntityDescription

When you write a static library which uses CoreData there's a big mess including a normal .xdatamodeld file into the project because you simply cannot just link its compiled version (.momd) into your binary, so it's better to create the whole…
12
votes
1 answer

objectID of NSManagedObject: is it consistent?

I need to have some unique&consistent ID for indexing data, I tried to use objectID of NSManagedObject, but looks like for the same entity, its objectID keeps changing, does anyone know if this is not consistent?
hzxu
  • 5,753
  • 11
  • 60
  • 95
11
votes
4 answers

How to insert new data to entity in Swift?

In Swift previously, I was able to use a code like this to add new data to my "TestEntity" in my data model. NSManagedObject was created for my "TestEntity" and I was able to set its attributes with the "dot" syntax At the end, I would save the…
11
votes
3 answers

How can I debug NSManagedObjects in XCode debugger?

How do you get to the values of your Entity (sub class of NSManaged Object) when in the XCode debugger? I get lost among the NSObject and _cd_XXX structures.
KingAndrew
  • 1,164
  • 4
  • 21
  • 41
11
votes
2 answers

Permanent NSManagedObjectID not so permanent?

I'm having trouble dealing with object IDs in CoreData. I'm using MagicalRecord for convenience and have 3 contexts: a private queue working context, a main queue context for UI and parent to the working context, and a private queue saving context…
brianpartridge
  • 763
  • 8
  • 19
10
votes
4 answers

adding non-persistent variables to nsmangedobject

I have a subclass of an NSManagedObject, and I'd like to add a couple ivars to keep track of some book-keeping. I don't want these vars to persist, and so that is why I don't include them as part of the data model. I'm having trouble finding the…
Wise Shepherd
  • 2,228
  • 4
  • 31
  • 43
10
votes
2 answers

Use NSManagedObject class without initWithEntity:?

My problem is similar to: Problem creating NSManagedObject derived class I have setup a NSManagedObject in Core Data and have a class for it. However, instead of creating an identical NSObject class, I'd like to use the NSManagedObject class, but I…
runmad
  • 14,846
  • 9
  • 99
  • 140
10
votes
1 answer

Why am I not able to override isEqual in my NSManagedObject subclass?

I have two custom NSManagedObject classes: Notes and Tags. When I override the isEqual: function in the Tags class I get an error message like this: 'Class 'Tags' for entity 'Tags' has an illegal override of NSManagedObject -isEqual:' Why is this…
leoyfm
  • 241
  • 4
  • 16
10
votes
2 answers

best practices - NSManagedObjectContextObjectsDidChangeNotification in iOS

I am writing my first comprehensive app using Core Data, and I want to see what the best way to keep track of various object changes / updates / deletes is. For example, I have a Notes entity and a Location entity, and a one-to-one relationship…
Z S
  • 7,039
  • 12
  • 53
  • 105
10
votes
2 answers

ViewContext not receiving updates from newBackgroundContext()

There is a similar question in stack overflow already but it doesn't work for me. There is a use case in my application where I have to observe the database changes to perform some operation. To receive updates I subscribed to …
10
votes
1 answer

SwiftyJSON: Converting Objects to JSON

I know with SwiftyJSON you can convert the objects from JSON to Swift. Does SwiftyJSON allows you to go back? i.e. take NSManagedObjects with relationships and converting them it into JSON? Example Please.
user1107173
  • 10,334
  • 16
  • 72
  • 117
10
votes
3 answers

Why doesn't NSManagedObject instances hold a strong reference to their NSManagedObjectContext?

As pointed out in another question on SO (and the Apple docs), NSManagedObject instances do not hold a strong reference to the NSManagedObjectContext from which they originated. On first blush, this seems like a strange decision, since…
10
votes
1 answer

Core data images from desktop to iphone

I built a simple mac data entry tool I use with an iPhone application. I've recently added thumbnail which I added via an Image Well using simple bindings. Its a transformable data type which seems to work fine. The iPhone application however won't…
user244434
  • 147
  • 2
  • 8