English 中文(简体)
核心数据衍生的属性是否与云层相适应?
原标题:Do Core Data derived attributes work properly with Cloudkit?
  • 时间:2023-09-04 01:51:01
  •  标签:
  • core-data

在我使用的情况下,我有两个实体:<代码>List 和Item,这两个实体具有一种任择性关系。 我愿在<代码>List上有两个非瞬间衍生特性:items.@countitems.@sum/code>。 这项工作吗?

为此,我认为,只要通过云盖的装置收到清单记录或相关项目记录,核心数据必须重新计算这些属性。 核心 情况如何?

如果这种情况自动发生,我就能够工作? 也许,我可以使用NSPersistentHistoryTrackingKey来核对每个新加入的List,并计算真实数字和数额,将其与最新的<代码>List entity中的数值相比较,如果出现错误,更新实体。 这可能会导致大量推迟更新和冲突。

谢谢。

问题回答

我理解你的问题。 这里的守则表明如何用核心数据和云基实现衍生特性:

import CoreData
import CloudKit

// Assuming you have List and Item entities with a one-to-many relationship

// List entity
class List: NSManagedObject {
    @NSManaged var items: Set<Item>
    @NSManaged var itemCount: Int16
    @NSManaged var itemSum: Double
}

// Item entity
class Item: NSManagedObject {
    @NSManaged var value: Double
}

// In your CoreData stack setup, enable history tracking
let description = NSPersistentStoreDescription()
description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "your.container.id")
description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)

// Whenever you update related Item records in Core Data, recalculate derived attributes
func updateDerivedAttributes(for list: List) {
    let itemCount = list.items.count
    let itemSum = list.items.reduce(0.0, { $0 + $1.value })
    
    list.itemCount = Int16(itemCount)
    list.itemSum = itemSum
    
    // Save your managedObjectContext to persist changes
}

// CloudKit subscription to monitor changes and trigger updates
let subscription = CKQuerySubscription(recordType: "List", predicate: NSPredicate(value: true), options: .firesOnRecordCreation)
let notificationInfo = CKSubscription.NotificationInfo()
notificationInfo.shouldSendContentAvailable = true
subscription.notificationInfo = notificationInfo

// Add the subscription to your CloudKit database
let container = CKContainer(identifier: "your.container.id")
let database = container.privateCloudDatabase
database.save(subscription) { subscription, error in
    // Handle subscription save completion
}

铭记利用云基的衍生特性,可能会带来复杂情况,特别是在yn和冲突方面。 你可能需要认真管理更新,以避免过度的冲突。 采用<代码>NSPersistentHistoryTrackingKey的做法可以发挥作用,但如果经常更新,则可能确实导致冲突。 必要时考虑采用解决冲突的战略。





相关问题
iPhone Core Data Recursive Relationships

I m having some trouble with a recursive relationship in core data. I ve got a managed object called a "SearchCategory", and that category can have "Categories" and it can also have a "Category." In ...

Plist Hierarchy and All Option

I have a string of data in a plist, which I ve got to display, hierarchically like this: Menu>Chapter>SubChapter>item>item details This might be super simple, in my initial menu, how would I have ...

Fetched Properties v Relationships (Core Data - iPhone)

I m a new iPhone developer (of about 4 months or so) who is starting to look at Core Data. In the "Beginning iPhone 3 Development" book by Dave Mark it mentions that the main difference between ...

Cocoa Core Data newbie how-tos

I am one of the great unwashed masses of .NET developers keen to try their hands at Mac OS X development. At the moment I am trying to figure out the various elements of Cocoa and getting a bit stuck ...

中间储存的核心数据

我想将核心数据作为输入数据库服务器更大数据集的切身。

Limit the returned resultset in CoreData

In CoreData, if I want to limit the returned resultset to 100, is it enough to just set the fetch limit to 100, or do I need to set the fetch batch size to 100 as well?

热门标签