Simplify record handling.

This commit is contained in:
Maurice Parker 2020-03-28 08:53:03 -05:00
parent fe2297213d
commit f59d6f712f
6 changed files with 23 additions and 156 deletions

View File

@ -68,9 +68,6 @@
51E490362288C37100C791F0 /* FeedbinDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E490352288C37100C791F0 /* FeedbinDate.swift */; };
51E4DB2E242633ED0091EB5B /* CloudKitZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E4DB2D242633ED0091EB5B /* CloudKitZone.swift */; };
51E4DB302426353D0091EB5B /* CloudKitAccountZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E4DB2F2426353D0091EB5B /* CloudKitAccountZone.swift */; };
51E4DB3224264B470091EB5B /* WebFeed+CloudKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E4DB3124264B470091EB5B /* WebFeed+CloudKit.swift */; };
51E4DB3424264CD50091EB5B /* Folder+CloudKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E4DB3324264CD50091EB5B /* Folder+CloudKit.swift */; };
51E4DB362426693F0091EB5B /* CloudKitRecordConvertable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E4DB352426693F0091EB5B /* CloudKitRecordConvertable.swift */; };
51E59599228C77BC00FCC42B /* FeedbinUnreadEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E59598228C77BC00FCC42B /* FeedbinUnreadEntry.swift */; };
51E5959B228C781500FCC42B /* FeedbinStarredEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51E5959A228C781500FCC42B /* FeedbinStarredEntry.swift */; };
552032F8229D5D5A009559E0 /* ReaderAPIEntry.swift in Sources */ = {isa = PBXBuildFile; fileRef = 552032ED229D5D5A009559E0 /* ReaderAPIEntry.swift */; };
@ -302,9 +299,6 @@
51E490352288C37100C791F0 /* FeedbinDate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinDate.swift; sourceTree = "<group>"; };
51E4DB2D242633ED0091EB5B /* CloudKitZone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitZone.swift; sourceTree = "<group>"; };
51E4DB2F2426353D0091EB5B /* CloudKitAccountZone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitAccountZone.swift; sourceTree = "<group>"; };
51E4DB3124264B470091EB5B /* WebFeed+CloudKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WebFeed+CloudKit.swift"; sourceTree = "<group>"; };
51E4DB3324264CD50091EB5B /* Folder+CloudKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Folder+CloudKit.swift"; sourceTree = "<group>"; };
51E4DB352426693F0091EB5B /* CloudKitRecordConvertable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CloudKitRecordConvertable.swift; sourceTree = "<group>"; };
51E59598228C77BC00FCC42B /* FeedbinUnreadEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinUnreadEntry.swift; sourceTree = "<group>"; };
51E5959A228C781500FCC42B /* FeedbinStarredEntry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinStarredEntry.swift; sourceTree = "<group>"; };
552032ED229D5D5A009559E0 /* ReaderAPIEntry.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReaderAPIEntry.swift; sourceTree = "<group>"; };
@ -520,11 +514,8 @@
51C034E0242D660D0014DC71 /* CKError+Extensions.swift */,
5103A9D82422546800410853 /* CloudKitAccountDelegate.swift */,
51E4DB2F2426353D0091EB5B /* CloudKitAccountZone.swift */,
51E4DB352426693F0091EB5B /* CloudKitRecordConvertable.swift */,
51C034DE242D65D20014DC71 /* CloudKitResult.swift */,
51E4DB2D242633ED0091EB5B /* CloudKitZone.swift */,
51E4DB3324264CD50091EB5B /* Folder+CloudKit.swift */,
51E4DB3124264B470091EB5B /* WebFeed+CloudKit.swift */,
);
path = CloudKit;
sourceTree = "<group>";
@ -1111,11 +1102,9 @@
51E490362288C37100C791F0 /* FeedbinDate.swift in Sources */,
9EEAE06E235D002D00E3FEE4 /* FeedlyGetCollectionsService.swift in Sources */,
5165D72922835F7A00D9D53D /* FeedSpecifier.swift in Sources */,
51E4DB3424264CD50091EB5B /* Folder+CloudKit.swift in Sources */,
9E85C8ED2367020700D0F1F7 /* FeedlyGetEntriesService.swift in Sources */,
9E5EC15923E01D8A00A4E503 /* FeedlyCollectionParser.swift in Sources */,
9E84DC492359A73600D6E809 /* FeedlyCheckpointOperation.swift in Sources */,
51E4DB3224264B470091EB5B /* WebFeed+CloudKit.swift in Sources */,
9E85C8EB236700E600D0F1F7 /* FeedlyGetEntriesOperation.swift in Sources */,
9E1D154D233370D800F4944C /* FeedlySyncAllOperation.swift in Sources */,
9E44C90F23C6FF3600CCC286 /* FeedlyIngestStreamArticleIdsOperation.swift in Sources */,
@ -1156,7 +1145,6 @@
51E59599228C77BC00FCC42B /* FeedbinUnreadEntry.swift in Sources */,
552032F8229D5D5A009559E0 /* ReaderAPIEntry.swift in Sources */,
552032FB229D5D5A009559E0 /* ReaderAPITag.swift in Sources */,
51E4DB362426693F0091EB5B /* CloudKitRecordConvertable.swift in Sources */,
5165D72822835F7800D9D53D /* FeedFinder.swift in Sources */,
9EBD49C023C67602005AD5CD /* FeedlyDownloadArticlesOperation.swift in Sources */,
51D58755227F53BE00900287 /* FeedbinTag.swift in Sources */,

View File

@ -18,6 +18,14 @@ final class CloudKitAccountZone: CloudKitZone {
let container: CKContainer
let database: CKDatabase
struct CloudKitWebFeed {
static let recordType = "WebFeed"
struct Fields {
static let url = "url"
static let editedName = "editedName"
}
}
init(container: CKContainer) {
self.container = container
self.database = container.privateCloudDatabase
@ -25,20 +33,13 @@ final class CloudKitAccountZone: CloudKitZone {
/// Persist a feed record to iCloud and return the external key
func createFeed(url: String, editedName: String?, completion: @escaping (Result<String, Error>) -> Void) {
let record = CKRecord(recordType: "Feed", recordID: generateRecordID())
record["url"] = url
let record = CKRecord(recordType: CloudKitWebFeed.recordType, recordID: generateRecordID())
record[CloudKitWebFeed.Fields.url] = url
if let editedName = editedName {
record["editedName"] = editedName
record[CloudKitWebFeed.Fields.editedName] = editedName
}
modify(recordsToStore: [record], recordIDsToDelete: []) { result in
switch result {
case .success:
completion(.success(record.recordID.recordName))
case .failure(let error):
completion(.failure(error))
}
}
save(recordToStore: record, completion: completion)
}
// private func fetchChangesInZones(_ callback: ((Error?) -> Void)? = nil) {

View File

@ -1,33 +0,0 @@
//
// CloudKitRecordConvertable.swift
// Account
//
// Created by Maurice Parker on 3/21/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import CloudKit
protocol CloudKitRecordConvertible {
static var cloudKitRecordType: String { get }
static var cloudKitZoneID: CKRecordZone.ID { get }
var cloudKitPrimaryKey: String { get }
var recordID: CKRecord.ID { get }
var cloudKitRecord: CKRecord { get }
func assignCloudKitPrimaryKeyIfNecessary()
}
extension CloudKitRecordConvertible {
public static var cloudKitRecordType: String {
return String(describing: self)
}
public var recordID: CKRecord.ID {
return CKRecord.ID(recordName: cloudKitPrimaryKey, zoneID: Self.cloudKitZoneID)
}
}

View File

@ -113,6 +113,17 @@ extension CloudKitZone {
// })
// }
public func save(recordToStore: CKRecord, completion: @escaping (Result<String, Error>) -> Void) {
modify(recordsToStore: [recordToStore], recordIDsToDelete: []) { result in
switch result {
case .success:
completion(.success(recordToStore.recordID.recordName))
case .failure(let error):
completion(.failure(error))
}
}
}
/// Sync local data to CloudKit
/// For more about the savePolicy: https://developer.apple.com/documentation/cloudkit/ckrecordsavepolicy
public func modify(recordsToStore: [CKRecord], recordIDsToDelete: [CKRecord.ID], completion: @escaping (Result<Void, Error>) -> Void) {

View File

@ -1,49 +0,0 @@
//
// Folder+CloudKit.swift
// Account
//
// Created by Maurice Parker on 3/21/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import CloudKit
extension Folder: CloudKitRecordConvertible {
enum CloudKitKey: String {
case name
}
static var cloudKitZoneID: CKRecordZone.ID {
return CloudKitAccountZone.zoneID
}
var cloudKitPrimaryKey: String {
return externalID!
}
var cloudKitRecord: CKRecord {
let record = CKRecord(recordType: Self.cloudKitRecordType)
record[.name] = name
return record
}
func assignCloudKitPrimaryKeyIfNecessary() {
if externalID == nil {
externalID = UUID().uuidString
}
}
}
extension CKRecord {
subscript(key: Folder.CloudKitKey) -> Any? {
get {
return self[key.rawValue]
}
set {
self[key.rawValue] = newValue as? CKRecordValue
}
}
}

View File

@ -1,51 +0,0 @@
//
// WebFeed+CloudKit.swift
// Account
//
// Created by Maurice Parker on 3/21/20.
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import CloudKit
extension WebFeed: CloudKitRecordConvertible {
enum CloudKitKey: String {
case url
case editedName
}
static var cloudKitZoneID: CKRecordZone.ID {
return CloudKitAccountZone.zoneID
}
var cloudKitPrimaryKey: String {
return externalID!
}
var cloudKitRecord: CKRecord {
let record = CKRecord(recordType: Self.cloudKitRecordType)
record[.url] = url
record[.editedName] = editedName
return record
}
func assignCloudKitPrimaryKeyIfNecessary() {
if externalID == nil {
externalID = UUID().uuidString
}
}
}
extension CKRecord {
subscript(key: WebFeed.CloudKitKey) -> Any? {
get {
return self[key.rawValue]
}
set {
self[key.rawValue] = newValue as? CKRecordValue
}
}
}