Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CKRecord initialization for EventModel and update SyncCoordinator to use the new initializer #3

Merged
merged 1 commit into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 24 additions & 25 deletions Sources/Scout/Core/Record.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,30 @@

import CloudKit

/// Converts an `EventModel` instance to a `CKRecord` for CloudKit storage.
///
/// This function takes an `EventModel` object and creates a new `CKRecord`
/// with the corresponding fields populated from the event.
///
/// - Parameter event: The `EventModel` instance to convert.
/// - Returns: A `CKRecord` populated with the event's data.
///
/// Note: The `version` field is set to 1 to indicate the initial version of the record.
/// This can be useful for handling migrations or updates to the record schema in the future.
///
func toRecord(event: EventModel) -> CKRecord {
let record = CKRecord(recordType: "Event")
extension CKRecord {

record["params"] = event.params
record["param_count"] = event.paramCount
record["name"] = event.name
record["level"] = event.level
record["date"] = event.date
record["hour"] = event.hour
record["week"] = event.week
record["uuid"] = event.uuid?.uuidString
record["version"] = 1
record["user_id"] = event.userID?.uuidString
record["session_id"] = event.sessionID?.uuidString
/// Initializes a new `CKRecord` instance with the specified `EventModel`.
///
/// This convenience initializer populates the record fields with the event data.
/// The `version` field is set to 1 to indicate the initial version of the record.
/// This can be useful for handling migrations or updates to the record schema in the future.
///
/// - Parameter event: The `EventModel` instance to use for populating the record.
/// - Returns: A new `CKRecord` instance populated with the event data.
///
convenience init(event: EventModel) {
self.init(recordType: "Event")

return record
self["params"] = event.params
self["param_count"] = event.paramCount
self["name"] = event.name
self["level"] = event.level
self["date"] = event.date
self["hour"] = event.hour
self["week"] = event.week
self["uuid"] = event.uuid?.uuidString
self["version"] = 1
self["user_id"] = event.userID?.uuidString
self["session_id"] = event.sessionID?.uuidString
}
}
2 changes: 1 addition & 1 deletion Sources/Scout/Core/SyncCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension SyncCoordinator {
try await database.save(matrix)

try await database.modifyRecords(
saving: group.events.map(toRecord),
saving: group.events.map(CKRecord.init),
deleting: []
)

Expand Down
Loading