Malleswari
Malleswari

Reputation: 1

Core data fetch is crashing with error "Fatal error: NSArray element failed to match the Swift Array Element type"

We have a coredata User entity with the schema as below -

name String
aliases Transformable

Note: aliases is an String array.

The fetch code is like this -

var userDetails:[User]?

let request = NSFetchRequest<NSFetchRequestResult>.init(entityName:"User")
userDetails = try self.viewContext.fetch(request) as? [User]

The fetch request is intermittently crashing with the below errors -

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

Expected User but found _NSCoreTypesetterLayoutCache

Fatal error: NSArrav element failed to match the Swift Arrav Element type

Expected NSFetchRequestResult but found NSAsynchronousFetchResult

Please let me know if anyone has any suggestions.

Upvotes: 0

Views: 320

Answers (1)

de.
de.

Reputation: 8517

Initialise your fetch request like this:

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

The fetchRequest method is auto-generated (unless you have opted out).
The key is putting your entity class in the angle brackets.

Upvotes: 0

Related Questions