Extending Protocols conforming to NSFetchRequestResult

Originator:J.osseiran
Number:rdar://27630979 Date Originated:31/07/2016
Status:Closed Resolved:
Product:Swift Product Version:3.0
Classification:Bug Reproducible:Yes
 
As of WWDC 2016 the Core Data team updated their framework to be friendlier to generics and the new beefed up `NSFetchRequest` now returns objects that conform to the `NSFetchRequestResult`. 

So in the code below I have my base protocol that conforms to the said `NSFetchRequestResult` protocol, and in an extension of its child protocol `ManagedObjectFetchable` I want to return fetch objects of conforming types:

    import CoreData

    @objc protocol ManagedObjectType: class, NSFetchRequestResult { }

    protocol ManagedObjectFetchable: ManagedObjectType { }

    extension ManagedObjectFetchable {
      static func preFetch(for predicate: Predicate, sortedBy sortDescriptors: [SortDescriptor]) throws -> [Self] {
        let fetchRequest = NSFetchRequest<Self>(entityName: Self.entityName)
        fetchRequest.predicate = predicate
        fetchRequest.sortDescriptors = sortDescriptors

        let dummyContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
        return try dummyContext.fetch(fetchRequest)
      }
    }

The problem here is this line:

    // Error: Binary operator '<' cannot be applied to operands of type 'NSFetchRequest<_>.Type' and 'Self.Type'
    let fetchRequest = NSFetchRequest<Self>(entityName: Self.entityName)

I don't think this has anything to do with binary operators but rather a bug in Swift

As pointed out by a Stackoverflow answer this code works:

    let fetchRequest: NSFetchRequest<Self> = NSFetchRequest(entityName: Self.entityName)

Stackoverflow link: http://stackoverflow.com/questions/38678318/extending-protocols-conforming-to-nsfetchrequestresult/38687263#38687263

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!