Joe Scotto
Joe Scotto

Reputation: 10877

What is the reason for having function for fetchRequest on an NSManagedObject class

On my app, I'm using manual code gen for one of my Core Data entities. When doing so, I had Xcode generate the class and extension files for me in which one of them had the following code block:

@nonobjc public class func fetchRequest() -> NSFetchRequest<Sensor> {
    return NSFetchRequest<Sensor>(entityName: "Sensor")
}

I can remove this and everything seems to work fine but I read in places that it's used by Core Data in order to function properly. So my question is: What exactly is this block doing and is it required for proper functionality to the entity?

Upvotes: 0

Views: 121

Answers (1)

vadian
vadian

Reputation: 285082

It is a convenience factory method to be able to create a fetch request type safe with

let request : NSFetchRequest<Sensor> = Sensor.fetchRequest()

along the lines of the convenience initializer syntax

let sensor = Sensor(context: context)

It's not required.

Upvotes: 1

Related Questions