Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Swift) (Select distinct) fatal error: NSArray element failed to match the Swift Array Element type #1311

Open
RomainPln opened this issue Jul 18, 2017 · 2 comments

Comments

@RomainPln
Copy link

RomainPln commented Jul 18, 2017

Hi, I want to find all distinct values of aPropertyName on MyEntity records, I wrote:

        let aRequest = MyEntity.mr_requestAllSorted(by: "aPropertyName", ascending: true)
        aRequest.propertiesToFetch = ["aPropertyName"]
        aRequest.returnsDistinctResults = true
        aRequest.resultType = .dictionaryResultType
        let testResult = MyEntity.mr_executeFetchRequest(aRequest)

As a result I have

fatal error: NSArray element failed to match the Swift Array Element type

Whatever I try to do with testResult.
Trying to cast testResult does not work and produces the same runtime crash.
(A workaround could be to map a classic request result but that's less efficient and elegant)

@thomasaw
Copy link

thomasaw commented Apr 6, 2018

Have you found a solution or workaround for this?

@thomasaw
Copy link

thomasaw commented Apr 6, 2018

My workaround:

extension NSManagedObject {
    // This has the function of MagicalRecord MR_executeFetchRequest, but is compatible with Swift
    static func fetch(_ request: NSFetchRequest<NSFetchRequestResult>) -> [Any]? {
        let context = NSManagedObjectContext.mr_default()
        var categoryObjects: [Any]?
        context.performAndWait() {
            do {
                categoryObjects = try context.fetch(request)
            } catch {
                categoryObjects = nil
            }
        }

        return categoryObjects
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants