Mám tento protokol:
protocol ManagedObjectProtocol {
associatedtype Entity
static var identifierKey: String { get }
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
func toEntity() -> Entity?
}
Potvrzuji, na některé z mých třídy NSManagedObject s příponou, jako je tento:
extension AppEntity: ManagedObjectProtocol {
typealias Entity = App
static var identifierKey: String {
return "articleId"
}
func toEntity() -> Entity? {
return nil
}
}
Když se snažím archiv s novou Xcode 13 dostanu tuto chybu:
Typ 'AppEntity' neodpovídá protokolu 'ManagedObjectProtocol'
Ale když se snažím spustit tento kód na svém zařízení se staví dobře.
To byla budova/archivace v pořádku s Xcode 12 a starší verze, ale s novými Xcode 13 (a Xcode 13.1) mám problém. Kde by mohl být problém? Jak to mohu opravit?
Jedna věc, že jsem se jako chyba, když archivace a ne, když debug build. Mám tento kód popředí se subjektem:
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))
}
}
}
}
Já si tyto chybové zprávy:
Typu "Jednotka", která má žádný členský 'fetchRequest'
Co je špatně? Jak mohu opravit tyto Základní chyby Dat při archivaci?
Díky za pomoc
Edit:
Jeden příklad za pomocnou metodu, ve které mám chybu s 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))
}
}
}