我创建了一个Core Data对象,如下所示:
@objc(Gates)
public class Gates : NSManagedObject {
public class func getFetchRequest() -> NSFetchRequest<Gates> {
let request = NSFetchRequest<Gates>(entityName: "Gates")
request.returnsObjectsAsFaults = false
return request
}
@NSManaged var updatedAt: String
@NSManaged var objectId: String
@NSManaged var identifier: String
@NSManaged var name: String
@NSManaged var address: String
@NSManaged var dueDate: String
@NSManaged var productionCode: String
@NSManaged var locationCountry: String
@NSManaged var locationCity: String
@NSManaged var locationBuilding: String
@NSManaged var locationLevel: String
@NSManaged var locationRoom: String
@NSManaged var locationRange: String
@NSManaged var isFavorite: Bool
public func setGateData(gateDict: [String: Any]) {
updatedAt = gateDict["updatedAt"] as? String ?? ""
objectId = gateDict["objectId"] as? String ?? ""
identifier = gateDict["identifier"] as? String ?? ""
name = gateDict["name"] as? String ?? ""
isFavorite = gateDict["isFavorite"] as? Bool ?? false
address = gateDict["address"] as? String ?? ""
dueDate = gateDict["dueDate"] as? String ?? ""
productionCode = gateDict["productionCode"] as? String ?? ""
locationCountry = gateDict["locationCountry"] as? String ?? ""
locationCity = gateDict["locationCity"] as? String ?? ""
locationBuilding = gateDict["locationBuilding"] as? String ?? ""
locationLevel = gateDict["locationLevel"] as? String ?? ""
locationRoom = gateDict["locationRoom"] as? String ?? ""
locationRange = gateDict["locationRange"] as? String ?? ""
}
}
我也在xcdatamodeld中设置了它:
现在,在我将对象保存在核心数据中之后,我正在使用属于类的一部分的getFetchRequest()
方法
request.returnsObjectsAsFaults = false
上的request
但是当我尝试打印提取的对象时,我仍然得到以下结果:
<Gates: 0x60c0000959a0> (entity: Gates; id: 0xd000000005e40000 <x-
coredata://B9C33A5D-BF96-433A-9186-F51AA253F488/Gates/p377> ; data: <fault>)
正如您在本案例中所看到的,数据仍为data: <fault>
。
为什么即使设置request.returnsObjectsAsFaults = false
也无法检索对象参数?我错过了什么?
答案 0 :(得分:0)
答案 1 :(得分:0)
我遇到了这个问题,我发现我没有在行中使用对象值,而是先用它初始化了一个变量,然后再使用该变量。
我想知道这是否是Core Data错误,或者我做错了什么。
public class Person: NSManagedObject, Identifiable {
@NSManaged public var firstName: String
@NSManaged public var lastName: String
@NSManaged public var emailAddress: String
}
这并非一直有效:
CoreDataManager.shared.persistentContainer.performBackgroundTask{ context in
context.automaticallyMergesChangesFromParent = true
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
do {
let email = "123Tester@gmail.com"
let request = Person.getPersonWith(email: email)
request.returnsObjectsAsFaults = false //DOES NOT WORK
if let person = try context.fetch(request).first{
print(person.fullName)
}
} catch{
fatalError()
}
}
但是这样做
CoreDataManager.shared.persistentContainer.performBackgroundTask{ context in
context.automaticallyMergesChangesFromParent = true
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
do {
let email = "123Tester@gmail.com"
let request = Person.getPersonWith(email: email)
if let person = try context.fetch(request).first{
let fullName = person.fullName
print(fullName)
}
} catch{
fatalError()
}
}