当我的应用程序首次启动时,我会做一些首次设置配置,它使用CoreData API为我的SQLLite数据库提供来自JSON文档的数据。
我首先从核心数据堆栈获取对托管对象上下文的引用,然后调用执行JSON配置的方法。然后我打电话给:
....(JSON conversion)....
context.perform {
_ = Exercise.insert(into: context, name: exerciseName, category: categoryNumber, equipment: equipment, skill: "", primaryMuscle: primaryMuscle, otherMuscles: otherMuscles, info: exerciseDescription, image1: exerciseImage1, image2: exerciseImage2)
}
在每次迭代时,我遍历JSON对象,因此它会持久保存每个对象。
插入对象的方法是:
public static func insert(into context: NSManagedObjectContext, name: String, category: Int16, equipment: String?, skill: String?, primaryMuscle: String?, otherMuscles: String?, info: String?, image1: String?, image2: String?) -> Exercise {
let exercise: Exercise = context.insertObject()
exercise.name = name
exercise.category = category
exercise.info = info
exercise.primaryMuscle = primaryMuscle
exercise.otherMuscles = otherMuscles
exercise.equipment = equipment
exercise.skill = skill
if image1 != nil {
exercise.image1Path = image1
}
if image2 != nil {
exercise.image2Path = image2
}
return exercise
}
然后我扩展了NSManagedObjectContext ...
extension NSManagedObjectContext {
public func insertObject<Object: NSManagedObject>() -> Object where Object: Managed {
guard let obj = NSEntityDescription.insertNewObject(forEntityName: Object.entityName, into: self) as? Object else { fatalError("Wrong object type") }
return obj
}
}
这是我对我&#34; Managed&#34;的声明。协议:
public protocol Managed: class, NSFetchRequestResult {
static var entityName: String {get}
....
}
extension Managed where Self: NSManagedObject {
public static var entityName: String { return entity().name! }
}
然后我试图从协议扩展中检索entityName时崩溃,我试图插入对象......
它给了我错误&#34;致命错误:在展开可选值时意外地发现了nil&#34;
我没有生成我的NSManagedObjectSublcasses,我自己创建了它们,但我确定我做得正确。我有一种感觉,虽然核心数据并没有拿起我的Model课程,因为#34;练习&#34;当应用程序运行时。这就是实体名称没有返回的原因。所以这是我的模型类的代码:
import UIKit
import CoreData
public class Exercise: NSManagedObject {
@NSManaged public fileprivate(set) var category: Int16
@NSManaged public fileprivate(set) var image1Path: String?
@NSManaged public fileprivate(set) var image2Path: String?
@NSManaged public fileprivate(set) var equipment: String?
@NSManaged public fileprivate(set) var info: String?
@NSManaged public fileprivate(set) var skill: String?
@NSManaged public fileprivate(set) var primaryMuscle: String?
@NSManaged public fileprivate(set) var otherMuscles: String?
public static let normalizedNameKey = "name_normalized"
@NSManaged fileprivate var primitiveName: String
public var name: String {
set {
willChangeValue(forKey: #keyPath(Exercise.name))
primitiveName = newValue
updateNormalizedName(newValue)
didChangeValue(forKey: #keyPath(Exercise.name))
}
get {
willAccessValue(forKey: #keyPath(Exercise.name))
let value = primitiveName
didAccessValue(forKey: #keyPath(Exercise.name))
return value
}
}
fileprivate func updateNormalizedName(_ name: String) {
setValue(name.normalizedForSearch, forKey: Exercise.normalizedNameKey)
}
public var exerciseImage1: UIImage? {
guard let image = image1Path else {return nil}
return UIImage(named: image)
}
public var exerciseImage2: UIImage? {
guard let image = image2Path else {return nil}
return UIImage(named: image)
}
public static func insert(into context: NSManagedObjectContext, name: String, category: Int16, equipment: String?, skill: String?, primaryMuscle: String?, otherMuscles: String?, info: String?, image1: String?, image2: String?) -> Exercise {
let exercise: Exercise = context.insertObject()
exercise.name = name
exercise.category = category
exercise.info = info
exercise.primaryMuscle = primaryMuscle
exercise.otherMuscles = otherMuscles
exercise.equipment = equipment
exercise.skill = skill
if image1 != nil {
exercise.image1Path = image1
}
if image2 != nil {
exercise.image2Path = image2
}
return exercise
}
}
extension Exercise: Managed {
public static var defaultSortDescriptors: [NSSortDescriptor] {
return [NSSortDescriptor(key: #keyPath(name), ascending: false)]
}
public static func defaultPredicate(muscleCategory: Int) -> NSPredicate
{
return NSPredicate(format: "%K == %ld",
#keyPath(Exercise.category), muscleCategory)
}
}
任何想法可能会出错?感谢
答案 0 :(得分:3)
我有完全相同的问题,使用完全相同的代码(Florian Kugler的核心数据手册)。
我添加新实体+关系后出现问题,创建新实体时,实体名称为nil 我通过删除实体并再次重新创建它来修复它。错误消失了。
答案 1 :(得分:2)
也许这不是一个好主意,但现在可以避免崩溃......等待更好的解决方案。
斯普利特4:extension NSObject {
var className: String {
return String(describing: type(of: self))
}
class var className: String {
return String(describing: self)
}
}
然后
static var entityName: String {
return self.className //insead of `entity().name!`
}
答案 2 :(得分:1)
答案 3 :(得分:0)
莫因,
首先解开可选值。
public static var entityName: String
{
if let _returnName = entity().name{
return _returnName
}else
{
return nil
}
}
}
CoreData:
课程练习
当您更改dbModel并执行“Create NSManagedObject Sub ..”时,练习中的所有函数都将被覆盖。
使用类似 ExerciseExtension
的单独类import UIKit
import CoreData
extension Exercise {
func setName(name : String)
{
self.name = name
}
.....
}
答案 4 :(得分:0)
在iOS 10.3+上使用Xcode 9.4.1的Swift 4.1 projet上,我遇到了类似的问题 我试图做这样的事情:
guard let description = Entity.entity(),
throw Error.entityNotFound
}
*[some code]*
context.perform {
*[some code]*
}
但是我的一些单元测试失败了,其他的则工作得很好。我的xcdatamodel
和我的NSManagedObject
子类都很好。
最后,我通过获取perform
闭包内的实体名称来使其工作:
context.perform {
guard let description = Entity.entity(),
throw Error.entityNotFound
}
*[some code]*
}