从 Core Data 加载 NSManagedObject 时,我是否免费获得它 children?
When loading an NSManagedObject from Core Data, do I get it's children for free?
我正在尝试学习如何在 Core Data 中使用这种奇特的新关系模式来模拟字符串数组。我有一个 Alarm 实体和一个 NotificationUuid 实体。 Alarm是NotificationUuid的parent个实体,因为一个Alarm可以有多个NotificationUuid,一个NotificationUuid只能有一个parent个Alarm。我在我的 .xcdatamodeld 文件中设置了所有这些。
我的问题是:当我像这样获取 parent 警报 object 时:
private func loadAlarms() {
os_log("loadAlarms() called", log: OSLog.default, type: .debug)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<AlarmMO>(entityName: "Alarm")
do {
if self.alarms.count == 0 {
self.alarms = try managedContext.fetch(fetchRequest)
os_log("Loading %d alarms", log: OSLog.default, type: .debug, self.alarms.count)
} else {
os_log("Didn't need to load alarms", log: OSLog.default, type: .debug)
}
} catch let error as NSError {
print("Could not fetch alarms. \(error), \(error.userInfo)")
}
}
我可以免费获得 AlarmMO(Alarm Managed Object)object 的 children、NotificationUuid object 吗?还是我现在也必须为他们设置一个获取请求?这整个奇妙的 parent/child 关系是如何运作的,我如何 set/load 来自这些实体的东西?
谢谢
我是这样定义 AlarmMO 的:
import CoreData
@objc(AlarmMO)
public class AlarmMO: NSManagedObject {
@NSManaged public var alarmNumber: Int64
@NSManaged public var alarmTime: NSDate?
@NSManaged public var endTimeInterval: Double
@NSManaged public var recurrence: Int64
@NSManaged public var startTimeInterval: Double
@NSManaged public var notificationUuidChildren: NSSet?
}
// MARK: Generated accessors for notificationUuidChildren
extension AlarmMO {
@objc(addNotificationUuidChildrenObject:)
@NSManaged public func addToNotificationUuidChildren(_ value: NotificationUuidMO)
@objc(removeNotificationUuidChildrenObject:)
@NSManaged public func removeFromNotificationUuidChildren(_ value: NotificationUuidMO)
@objc(addNotificationUuidChildren:)
@NSManaged public func addToNotificationUuidChildren(_ values: NSSet)
@objc(removeNotificationUuidChildren:)
@NSManaged public func removeFromNotificationUuidChildren(_ values: NSSet)
}
和 NotificationUuidMO:
import CoreData
@objc(NotificationUuid)
public class NotificationUuidMO: AlarmMO {
@NSManaged public var notificationUuid: String
@NSManaged public var alarmParent: AlarmMO
}
当看到你的 AlarmMO
模型时,你可以只获取你的 AlarmMO
模型,它将保存 notificationUuidChildren
集合中 NotificationUuidMO
的列表。所以不需要单独取NotificationUuidMO
而AlarmMO到NotificationUuidMO
是一对多的关系。所以你可以从 AlarmMO
得到 notificationUuidChildren
,从 NotificationUuidMO
.
得到 alarmParent
要将 NotificationUuidMO
添加到 notificationUuidChildren
集,您可以使用 extension AlarmMO
.
中给出的 Core-Data 生成的访问器
示例:
let notificationUuid = NotificationUuidMO....
let alarmMO = AlarmMO....
alarmMO.addToNotificationUuidChildren(notificationUuid)//Here your notificationUuid will be added to `notificationUuidChildren` Set and `alarmParent` of your `notificationUuid` will be automatically assigned to `alarmMO`.
我正在尝试学习如何在 Core Data 中使用这种奇特的新关系模式来模拟字符串数组。我有一个 Alarm 实体和一个 NotificationUuid 实体。 Alarm是NotificationUuid的parent个实体,因为一个Alarm可以有多个NotificationUuid,一个NotificationUuid只能有一个parent个Alarm。我在我的 .xcdatamodeld 文件中设置了所有这些。
我的问题是:当我像这样获取 parent 警报 object 时:
private func loadAlarms() {
os_log("loadAlarms() called", log: OSLog.default, type: .debug)
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
let fetchRequest = NSFetchRequest<AlarmMO>(entityName: "Alarm")
do {
if self.alarms.count == 0 {
self.alarms = try managedContext.fetch(fetchRequest)
os_log("Loading %d alarms", log: OSLog.default, type: .debug, self.alarms.count)
} else {
os_log("Didn't need to load alarms", log: OSLog.default, type: .debug)
}
} catch let error as NSError {
print("Could not fetch alarms. \(error), \(error.userInfo)")
}
}
我可以免费获得 AlarmMO(Alarm Managed Object)object 的 children、NotificationUuid object 吗?还是我现在也必须为他们设置一个获取请求?这整个奇妙的 parent/child 关系是如何运作的,我如何 set/load 来自这些实体的东西?
谢谢
我是这样定义 AlarmMO 的:
import CoreData
@objc(AlarmMO)
public class AlarmMO: NSManagedObject {
@NSManaged public var alarmNumber: Int64
@NSManaged public var alarmTime: NSDate?
@NSManaged public var endTimeInterval: Double
@NSManaged public var recurrence: Int64
@NSManaged public var startTimeInterval: Double
@NSManaged public var notificationUuidChildren: NSSet?
}
// MARK: Generated accessors for notificationUuidChildren
extension AlarmMO {
@objc(addNotificationUuidChildrenObject:)
@NSManaged public func addToNotificationUuidChildren(_ value: NotificationUuidMO)
@objc(removeNotificationUuidChildrenObject:)
@NSManaged public func removeFromNotificationUuidChildren(_ value: NotificationUuidMO)
@objc(addNotificationUuidChildren:)
@NSManaged public func addToNotificationUuidChildren(_ values: NSSet)
@objc(removeNotificationUuidChildren:)
@NSManaged public func removeFromNotificationUuidChildren(_ values: NSSet)
}
和 NotificationUuidMO:
import CoreData
@objc(NotificationUuid)
public class NotificationUuidMO: AlarmMO {
@NSManaged public var notificationUuid: String
@NSManaged public var alarmParent: AlarmMO
}
当看到你的 AlarmMO
模型时,你可以只获取你的 AlarmMO
模型,它将保存 notificationUuidChildren
集合中 NotificationUuidMO
的列表。所以不需要单独取NotificationUuidMO
而AlarmMO到NotificationUuidMO
是一对多的关系。所以你可以从 AlarmMO
得到 notificationUuidChildren
,从 NotificationUuidMO
.
alarmParent
要将 NotificationUuidMO
添加到 notificationUuidChildren
集,您可以使用 extension AlarmMO
.
示例:
let notificationUuid = NotificationUuidMO....
let alarmMO = AlarmMO....
alarmMO.addToNotificationUuidChildren(notificationUuid)//Here your notificationUuid will be added to `notificationUuidChildren` Set and `alarmParent` of your `notificationUuid` will be automatically assigned to `alarmMO`.