Bu Protokol var :
protocol ManagedObjectProtocol {
associatedtype Entity
static var identifierKey: String { get }
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
func toEntity() -> Entity?
}
NSManagedObject sınıflarımdan bazılarına bunun gibi bir uzantı ile onaylıyorum:
extension AppEntity: ManagedObjectProtocol {
typealias Entity = App
static var identifierKey: String {
return "articleId"
}
func toEntity() -> Entity? {
return nil
}
}
Yeni Xcode 13 ile arşivlemeye çalıştığımda bu hatayı alıyorum:
'AppEntity 'türü'ManagedObjectProtocol' protokolüne uymuyor
Ancak bu kodu cihazımda çalıştırmaya çalıştığımda iyi çalışıyor.
Xcode 12 ve daha eski sürümlerle iyi bir şekilde inşa ediyor / arşivliyordu, ancak yeni Xcode 13 (ve Xcode 13.1) ile sorunum var. Nerede sorun olabilir? Bunu nasıl düzeltebilirim?
Hata ayıklarken değil, arşivlerken hata olarak aldığım bir şey daha var. Varlık almak için bu kodum var:
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))
}
}
}
}
Bu hata mesajlarını alıyorum:
'Varlık' türünde 'fetchRequest'üyesi yok
Sorun ne? Arşivleme sırasında bu Temel Veri hatalarını nasıl düzeltebilirim?
Yardım için teşekkürler
Düzenlemek:
Hata aldığım yardımcı yöntem için bir örnek daha 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))
}
}
}