Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data: What difference between insertNewObject vs designated initializer

I have found two solutions to the problem:

public class MyClass: NSManagedObject {
    init(_ entity:NSEntityDescription, dict: NSDictionary, context: NSManagedObjectContext) {
        super.init(entity: entity , insertInto: context)
    }
}

let entity = NSEntityDescription.entity(forEntityName: "MyClass", in: self.context!)
_ = MyClass.init(entity!, dict: item as! NSDictionary, context: self.context!)

and

let myClass = NSEntityDescription.insertNewObject(forEntityName: "MyClass", into: context) as! MyClass

but I cannot understand the difference in the end. And how does it affect NSManagedObjectContext?

like image 956
Yaroslav Tytarenko Avatar asked May 07 '26 05:05

Yaroslav Tytarenko


1 Answers

The have the same effect. The method on NSEntityDescription is a "factory" method, which you don't see too often in Objective-C (that method existed before Swift did). But the end result is the same as using the designated initializer. Although the factory method's code is not available, you can assume that it calls the designated initializer at some point.

like image 198
Tom Harrington Avatar answered May 08 '26 19:05

Tom Harrington



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!