Post

Replies

Boosts

Views

Activity

SwiftData #Predicate cannot test for nil relationship
In Xcode Version 15.0 beta (15A5160n) this predicate will not build: let thisPredicate = #Predicate<Ledgers> { ($0.myStore != nil) } The definition of .myStore in the Ledgers class is: var myStore: Stores? an optional relationship. Is there anyway to test for nil optional relationships in SwiftData? The inverse relationship in Stores is: @Relationship(.deny, inverse: \Ledgers.myStore) var myReceipts: [Ledgers]
10
4
2.4k
Jun ’23
How to make SwiftData class Codable
Consider the Categories class below. Without the @Model line, Xcode has no problems. As soon as the @Model line is added, Xcode protests that the class does not conform to Decodable or Encodable. Apparently the @Model macro expanded macro prevents the implied boilerplate that the systems adds when the Codable protocol is added to the class. I've filed feedback (FB12444837) for this. Does anyone have suggestions that will let me avoid the boilerplate? This is the simplest class in my schema, I've got 12 other classes with many more variables. @Model class Categories: Identifiable, Codable { // MARK: Identifiable var id: UUID { return uuidKey } // MARK: - Properties @Attribute(.unique) var uuidKey: UUID = UUID() var dateCreated: Date = Date() var dateModified: Date = Date() var dateRealm: Date? = nil var uuidUser: UUID = UUID() var uuidFamily: UUID = UUID() var myName: String = "" }
5
7
2.6k
Jun ’23
CKSyncEngine.RecordZoneChangeBatch and the CKSyncEngineDelegate protocol
I'm having some trouble with the following function from the CKSyncEngineDelegate protocol. func nextRecordZoneChangeBatch(_ context: CKSyncEngine.SendChangesContext, syncEngine: CKSyncEngine) async -> CKSyncEngine.RecordZoneChangeBatch? { The sample code from the documentation is func nextRecordZoneChangeBatch( _ context: CKSyncEngine.SendChangesContext, syncEngine: CKSyncEngine ) async -> CKSyncEngine.RecordZoneChangeBatch? { // Get the pending record changes and filter by the context's scope. let pendingChanges = syncEngine.state.pendingRecordZoneChanges .filter { context.options.zoneIDs.contains($0) } // Return a change batch that contains the corresponding materialized records. return await CKSyncEngine.RecordZoneChangeBatch( pendingChanges: pendingChanges) { self.recordFor(id: $0) } } init?(pendingChanges: [CKSyncEngine.PendingRecordZoneChange], recordProvider: (CKRecord.ID) -> (CKRecord?)) works fine for the sample app which only has one record type, but it seems incredible inefficient for my app which has a dozen different record types. The recordProvider gives you a CKRecord.ID, but not the CKRecord.RecordType. Searching each record type for a matching ID seems very inefficient. Doesn't the CKSyncEngine.PendingRecordZoneChange contain an array of CKRecords, not just CKRecord.IDs? According to the documentation CKSyncEngine.RecordZoneChangeBatch has a recordsToSave property, but Xcode reports 'CKSyncEngine.PendingRecordZoneChange' has no member 'recordsToSave' I'm looking for someway to get the CKRecords from syncEngine.state.pendingRecordZoneChanges.
3
1
552
Feb ’24