I Dati principali di errori in caso di archiviazione con il nuovo Xcode 13

0

Domanda

Ho questo protocollo:

protocol ManagedObjectProtocol {
    associatedtype Entity
    
    static var identifierKey: String { get }
    static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
    
    func toEntity() -> Entity?
}

Confermo per alcuni dei miei NSManagedObject classi con estensione come questa:

extension AppEntity: ManagedObjectProtocol {
    typealias Entity = App
    
    static var identifierKey: String {
        return "articleId"
    }
    
    func toEntity() -> Entity? {
        return nil
    }
}

Quando cerco di archivio con il nuovo Xcode 13 ottengo questo errore:

Tipo 'AppEntity' non è conforme al protocollo 'ManagedObjectProtocol'

Ma quando cerco di eseguire questo codice sul mio dispositivo si costruisce bene.

Era costruzione/archiviazione bene con Xcode 12 e versioni precedenti, ma con il nuovo Xcode 13 (e Xcode 13.1), ho un problema. Dove potrebbe essere il problema? Come posso risolvere il problema?

Una cosa che ottengo come errore durante l'archiviazione e non quando build di debug. Ho questo codice ribalta ottenere entità:

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))
            }
        }
    }
}

Ricevo questi messaggi di errore:

Tipo di 'Entità' non ha nessun membro 'fetchRequest'

Cosa c'è di sbagliato? Come posso risolvere questi i Dati principali di errori durante l'archiviazione?

Grazie per l'aiuto

Edit:

Un altro esempio per il metodo di supporto che ricevo errore con 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))
        }
    }
}
core-data objective-c protocols swift
2021-10-26 08:08:33
1

Migliore risposta

1

Ho avuto lo stesso errori di compilazione. Non riesco a trovare un errore di compilazione ragione, ma credo che questo sia uno di risolvere il problema:

if let entityName = Entity.entity().name {
    let request = NSFetchRequest<Entity>(entityName: entityName)
    ....
}
2021-10-27 11:11:31

Sì, questa è la soluzione che io possa archivio di progetto e di lavoro e per ora lo sto usando. Ma vorrei avere una reale soluzione, perché non ho motivo perché ho questo errore di compilazione e non so se potrebbe significare più problemi in futuro.
Libor Zapletal

Sono totalmente d'accordo con te
kumabook

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................