3

Having a strange problem with generics on Swift

have a NSManagedObject subclasses hierarchy with extensions:

BaseCloudKitItem<-MyObject

extension BaseCloudKitItem {
    func updateDataWithCKRecord(record: CKRecord!, inManagedObjectContext context: NSManagedObjectContext!) {
        cloudKitRecordID = record?.recordID.recordName
        cloudKitType = record?.recordType
        println("\(self) updateDataWithCKRecord:inManagedObjectContext: called")
    }
}

extension MyObject {
    override func updateDataWithCKRecord(record: CKRecord!, inManagedObjectContext context: NSManagedObjectContext!) {
        super.updateDataWithCKRecord(record, inManagedObjectContext: context)
        title = record?.objectForKey("title") as? NSString
    }
}

have a generic class:

class CloudFetcher<T : BaseCloudKitItem> {
    class func someFetchingAndMappingMethod(completion:(() -> Void)?) {
         someFetchingMethodWithCompletion { (records, error) in
             if let err = error {
                 completion?(nil, err)
             } else {
                 NSManagedObjectContext.saveDataInBackgroundWithBlock({ (localContext) in
                     T.deleteAllInContext(localContext)
                     for record in records {
                         let object = T.createEntityInContext(localContext) as T
                         object.updateDataWithCKRecord(record, inManagedObjectContext: localContext)
                     }
                     }, completion: completion)
                 }
             }
        }
    }

and usage:

CloudFetcher<MyObject>.someFetchingAndMappingMethod { _ in

}

Passed class into generic CloudFetcher is MyObject

So the problem is I discovered that methods called inside generic from the base class BaseCloudKitItem, not from MyObject. If I change the CloudFetcher interface declaration from class CloudFetcher<T : BaseCloudKitItem> to class CloudFetcher<T : MyObject> it works perfect, but there is no point to use generics this way.

Also, in logs from println() inside target method I can see needed class, but needed method is still not getting called:

<MyObject: 0x7aab7130> (entity: MyObject; id: 0x802f5bf0 <x-coredata:///InstrumentToken/t1AAF2438-9DF7-44DA-89B2-C3C1BE3D91FE17> ; data: {
    cloudKitRecordID = "fb4fac88-40e5-4aef-af3c-6e36867dbf5f";
    cloudKitType = MyObject;
    title = nil;
}) updateDataWithCKRecord:inManagedObjectContext: called

Thats looks pretty strange for me, maybe somebody can offer me someway I can solve it?

Thanks!

iiFreeman
  • 5,165
  • 2
  • 29
  • 42

0 Answers0