我有这样的协议:
protocol ManagedObjectProtocol {
associatedtype Entity
static var identifierKey: String { get }
static func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>
func toEntity() -> Entity?
}
我确认我的一些十分整洁类扩展这样的:
extension AppEntity: ManagedObjectProtocol {
typealias Entity = App
static var identifierKey: String {
return "articleId"
}
func toEntity() -> Entity? {
return nil
}
}
当我试图归档与新的低13我得到这个错误:
类型'AppEntity'不符合协议'ManagedObjectProtocol'
但是当我只是尝试运行这个代码在我的装置它建立的罚款。
它是建筑/存档的现有模式12岁以上的版本,但与新的低13(和低13.1)我有问题。 在可能是问题吗? 我如何能解决这个问题?
一件事我得到了错误的时候归档和不当debug build。 我有这个代码前获得的实体:
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))
}
}
}
}
我得到这些错误信息:
类型的'实体'没有件'fetchRequest'
怎么了? 我怎样才能解决这些核心数据的错误,当存档?
谢谢你的帮助
编辑:
一个例子为辅助方法中,我获得的错误 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))
}
}
}