How to make a designated initializer for NSManagedObject subclass in Swift?

A convenience initializer must call the designated initializer on self: convenience init(text: String, isCorrect: Bool, entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext!) { self.init(entity: entity, insertIntoManagedObjectContext: context) self.text = text self.isCorrect = isCorrect } which would be called as let newAlternative = Alternative(text: “third platform”, isCorrect: true, entity: entityDescription, insertIntoManagedObjectContext: managedObjectContext) In addition, you could also move … Read more

CoreData could not fulfill a fault for

Hmm. Are you properly implementing concurrency following this guide? The problem you are seeing is a common one when using core data across multiple threads. Object was deleted in your “background context”, while then it’s being accessed by another context. Calling [context processPendingChanges] on your background context after the delete but before the save may … Read more

Core-Data iPhone: could not locate an NSManagedObjectModel

Ok. Placing the following code inside of the RootViewController’s viewDidLoad eliminates the error: if (managedObjectContext == nil) { managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; NSLog(@”After managedObjectContext: %@”, managedObjectContext); } I found someone with a similar problem here on SO: link text As Aryeh pointed out in that post: “In short you are trying to … Read more

Why Xcode does not show index options for CoreData entities and attributes?

As @tomharrington said, the solution is in the WWDC 2017 video: Indexing stuff begins at 10:32 and the demo at 16:40. It’s still possible to create and index via Xcode9: First, select your entity, long-click on the + (Add Entity), and select Add Fetch Index. Then, name your index appropriately, the select the property you … Read more

Setting an NSManagedObject relationship in Swift

As of Xcode 7 and Swift 2.0 (see release note #17583057), you are able to just add the following definitions to the generated extension file: extension PersonModel { // This is what got generated by core data @NSManaged var name: String? @NSManaged var hairColor: NSNumber? @NSManaged var parents: NSSet? // This is what I manually … Read more

Subclass.fetchRequest() Swift 3.0, extension not really helping 100%?

The reason this is happening is there are two methods named fetchRequest in your Person class: first is in your model subclass (Person) with return type NSFetchRequest<Person> second is in NSManagedObject with return type NSFetchRequest<NSFetchRequestResult> That’s actually why it is ambiguous, compiler does not know which out of 2 you refer to. If you rename … Read more

Core Data’s NSPrivateQueueConcurrencyType and sharing objects between threads

When you use NSPrivateQueueConcurrencyType you need to do anything that touches that context or any object belonging to that context inside the -performBlock: method. Your code above is illegal since you’re passing those objects back to the main queue. The new API helps you in solving this, though: You create one context that’s associated with … Read more

How to use Core Data’s ManagedObjectModel inside a framework?

I’m a bit late for flohei’s issue, but hopefully this helps anybody else who wanders by. It is possible to get this to work without having to perform script-fu to copy resources around! By default Apple’s Core Data template gives you something like this: lazy var managedObjectModel: NSManagedObjectModel = { let modelURL = NSBundle.mainBundle().URLForResource(“MyCoreDataModel”, withExtension: … Read more

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