I have this protocol:
protocol ManagedObjectProtocol {
associatedtype Entity
static var identifierKey: String { get }
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
func toEntity() -> Entity?
}
I confirm to some of my NSManagedObject classes with extension like this:
extension AppEntity: ManagedObjectProtocol {
typealias Entity = App
static var identifierKey: String {
return "articleId"
}
func toEntity() -> Entity? {
return nil
}
}
When I try to archive with new Xcode 13 I get this error:
Type 'AppEntity' does not conform to protocol 'ManagedObjectProtocol'
But when I just try to run this code on my device it builds fine.
It was building/archiving fine with Xcode 12 and older versions but with new Xcode 13 (and Xcode 13.1) I have problem. Where could be problem? How can I fix it?
One more thing that I get as error when archiving and not when debug build. I have this code fore get entity:
func get<Entity: NSManagedObject>
(with predicate: NSPredicate? = nil,
sortDescriptors: [NSSortDescriptor]? = nil,
fetchLimit: Int? = nil,
inContext context: NSManagedObjectContext? = nil,
completion: @escaping (Result<[Entity], Error>) -> Void) {
if let ctx = context {
coreData.performTask({ (context) in
do {
let fetchRequest = Entity.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
if let fetchLimit = fetchLimit {
fetchRequest.fetchLimit = fetchLimit
}
let results = try context.fetch(fetchRequest) as? [Entity]
completion(.success(results ?? []))
} catch {
completion(.failure(error))
}
}, inContext: ctx)
} else {
coreData.performForegroundTask { context in
do {
let fetchRequest = Entity.fetchRequest()
fetchRequest.predicate = predicate
fetchRequest.sortDescriptors = sortDescriptors
if let fetchLimit = fetchLimit {
fetchRequest.fetchLimit = fetchLimit
}
let results = try context.fetch(fetchRequest) as? [Entity]
completion(.success(results ?? []))
} catch {
completion(.failure(error))
}
}
}
}
I get these error messages:
Type 'Entity' has no member 'fetchRequest'
What's wrong? How can I fix these Core Data errors when archiving?
Thanks for help
Edit:
One more example for helper method in which I get error with no member fetchRequest
:
func count(entity: NSManagedObject.Type,
with predicate: NSPredicate? = nil,
fetchLimit: Int? = nil,
completion: @escaping (Result<Int, Error>) -> Void) {
coreData.performForegroundTask { context in
do {
let fetchRequest = entity.fetchRequest()
fetchRequest.predicate = predicate
if let fetchLimit = fetchLimit {
fetchRequest.fetchLimit = fetchLimit
}
let count = try context.count(for: fetchRequest)
completion(.success(count))
} catch {
completion(.failure(error))
}
}
}