invalid redeclaration in auto code generate NSManagedObject Subclass Swift 3

In Xcode 8.1, before using the auto code generator, you must select the entity in your data model: Then go to the Data Model Inspector tab: Under “Codegen” select “Manual/Node” After that you could create a NSManagedObject subclass without errors. Alternatively, if you have already used ‘Class Definition’, you can go into your existing .xcdatamodeld … Read more

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

The optional checkbox in the Core Data Model Editor has been existing before Swift and its optionals where introduced. Apple states about it in its Core Data Programming Guide: You can specify that an attribute is optional — that is, it is not required to have a value. In general, however, you are discouraged from … Read more

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

First, having more than one NSManagedObjectContext on a single thread is not a standard configuration. 99% of the time you only need one context and that will solve this situation for you. Why do you feel you need more than one NSManagedObjectContext? Update That is actually one of the few use cases that I have … Read more

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

You will want to listen for the NSManagedObjectContextObjectsDidChangeNotification to pick up all changes to your data model. This can be done using code like the following: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDataModelChange:) name:NSManagedObjectContextObjectsDidChangeNotification object:myManagedObjectContext]; which will trigger -handleDataModelChange: on any changes to the myManagedObjectContext context. Your -handleModelDataChange: method would look something like this: – (void)handleDataModelChange:(NSNotification *)note { … Read more

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

For what it’s worth, Marcus Zarra seems to be promoting the nil context approach, claiming that it’s expensive to create a new context. For more details, see this answer to a similar question. Update I’m currently using the nil context approach and have encountered something that might be of interest to others. To create a … Read more

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

Here’s a class I created to perform a “deep copy” of managed objects: attributes and relationships. Note that this does not check against loops in the object graph. (Thanks Jaanus for the starting off point…) @interface ManagedObjectCloner : NSObject { } +(NSManagedObject *)clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context; @end @implementation ManagedObjectCloner +(NSManagedObject *) clone:(NSManagedObject *)source inContext:(NSManagedObjectContext *)context{ … Read more

Xcode 8 generates broken NSManagedObject subclasses for iOS 10

I finally got mine to work. Here is what I did. (Flights is one of my entities) I setup the xcdatamodeld as follows And then the entity as Then I used Editor -> Create NSManagedObject Subclass This creates two files for my flights entity Flights+CoreDataProperties.swift Flights+CoreDataClass.swift I renamed Flights+CoreDataClass.swift to Flights.swift Flights.swift is just import … Read more

Multiple NSEntityDescriptions Claim NSManagedObject Subclass

Post-automatic-caching This should not happen anymore with NSPersistent[CloudKit]Container(name: String), since it seems to cache the model automatically now (Swift 5.1, Xcode11, iOS13/MacOS10.15). Pre-automatic-caching NSPersistentContainer/NSPersistentCloudKitContainer does have two constructors: init(name: String) init(name: String, managedObjectModel model: NSManagedObjectModel) The first is just a convenience initializer calling the second with a model loaded from disk. The trouble is that … Read more

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

Checking the context of the managed object seems to work: if (managedObject.managedObjectContext == nil) { // Assume that the managed object has been deleted. } From Apple’s documentation on managedObjectContext … This method may return nil if the receiver has been deleted from its context. If the receiver is a fault, calling this method does … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)