1

I'm doing some refactoring to move code to using Promises (using Hydra) instead of async callbacks. I originally had this method and it worked fine:

static func fetch<D: AnyDTO, E: AnyEntity>(
    _ context      : NSManagedObjectContext, 
    fetch request  : NSFetchRequest<E>, 
    successHandler : @escaping ([D]) -> (), 
    errorHandler   : @escaping ErrorHandler)

So I changed this to work with promises like this:

import Hydra


static func fetch<D: AnyDTO, E: AnyEntity>(
    _ context     : NSManagedObjectContext, 
    fetch request : NSFetchRequest<E>) -> Promise<[D]> 
{
    return Promise<[D]>(in: .background) { resolve, reject, _ in
    ...
    }
}

with client code trying to call the function like this:

let request: NSFetchRequest<Location> = Location.fetchRequest()
return CacheUtils.fetch(context, fetch: request)

But the compiler is giving me this error:

Cannot convert value of type 'NSFetchRequest' to expected argument type 'NSFetchRequest<_>'

and I'm unsure why. I've checked out similar questions, and noticed the issue of using a concrete type within the function (see this). I think Promise might fit this bill, but on the other hand, the Promise is generic, so I'm not confident that is the problem. Is it possible to do what I'm trying to achieve in Swift?

jerney
  • 2,187
  • 1
  • 19
  • 31
  • [This](https://stackoverflow.com/questions/53746921/swift-cant-infer-generic-type-when-generic-type-is-being-passed-through-a-param) should help. Read the comments too. Specifically [this](https://stackoverflow.com/questions/53746921/swift-cant-infer-generic-type-when-generic-type-is-being-passed-through-a-param#comment94348934_53747719) one. PS: Always share the link of 1-2 questions you've looked into. We don't know which one you did and which you didn't look into – mfaani Feb 07 '19 at 00:35
  • 1
    Thx, reading now, and will edit w/ what I've checked out so far. – jerney Feb 07 '19 at 00:40

0 Answers0