nsmanagedobject
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
NSNull handling for NSManagedObject properties values
It might be a little easier if you wrap this in a macro: #define NULL_TO_NIL(obj) ({ __typeof__ (obj) __obj = (obj); __obj == [NSNull null] ? nil : obj; }) Then you can write things like fight.winnerID = NULL_TO_NIL([dict objectForKey:@”winner”]); Alternatively you can pre-process your dictionary and replace all NSNulls with nil before even trying … 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
Duplicate symbol error when adding NSManagedObject subclass, duplicate link
If you do not generate managed object subclass automatically, then don’t forget to check “Codegen” settings for an Entity in Data Model Inspector:
Is it possible to override getters and setters for @dynamic properties in an NSManagedObject subclass?
Never call [super valueForKey:..] in a NSManagedObject subclass! (unless you implemented them in a superclass yourself) Instead use the primitive accessor methods. Sample getter/setter implementation from ADC: @interface Department : NSManagedObject @property(nonatomic, strong) NSString *name; @end @interface Department (PrimitiveAccessors) – (NSString *)primitiveName; – (void)setPrimitiveName:(NSString *)newName; @end @implementation Department @dynamic name; – (NSString *)name { [self … Read more
Generating Swift models from Core Data entities
Lets have a look on the Objective-C way: Person.h (Header-File) #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Person : NSManagedObject @property (nonatomic, retain) NSString *name; @end Person.m (Implementation-File) #import “Person.h” @implementation Person @dynamic name; @end Swift The documentation already included in Xcode6-Beta says: Core Data provides the underlying storage and implementation of properties in subclasses of the … Read more
‘filenames are used to distinguish private declarations of the same name’ error
The issue is happening because of the Xcode automatic subclass generation feature. According to What’s New In Core Data Xcode automatic subclass generation Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector: Manual/None is the default, and previous behavior; in this case you should implement your own subclass … Read more
Core Data primary key ID for a row in the database
-[NSManagedObject objectID] is the unique ID for an object instance in Core Data. It can be serialized via -[NSManagedObjectID URIRepresentation]. You can retrieve the objectID from a persistent store coordinator with -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:] and then get the object from a managed object context with -[NSManagedObjectContext objectWithID:]. BUT You should keep in mind that Core Data … Read more