gpt4 book ai didi

ios - returnsObjectsAsFaults 未按预期工作

转载 作者:行者123 更新时间:2023-11-28 07:52:00 24 4
gpt4 key购买 nike

我创建了一个核心数据对象如下:

@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 中进行了设置:

enter image description here

现在,在我将对象保存到核心数据中并使用 getFetchRequest() 之后作为设置的类的一部分的方法 request.returnsObjectsAsFaults = falserequest 上但是当我尝试打印获取的对象时,我仍然得到以下结果:

 <Gates: 0x60c0000959a0> (entity: Gates; id: 0xd000000005e40000 <x-
coredata://B9C33A5D-BF96-433A-9186-F51AA253F488/Gates/p377> ; data: <fault>)

如您所见,在这种情况下,数据仍然是 data: <fault> .

为什么即使我设置了 request.returnsObjectsAsFaults = false 也没有检索到对象参数?我错过了什么?

最佳答案

我遇到了这个问题,我发现在我的案例中我没有使用行中的对象值,而是先用它初始化一个变量然后使用该变量。

我很想知道这是 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()
}
}

关于ios - returnsObjectsAsFaults 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49415076/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com