-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParent.swift
More file actions
110 lines (85 loc) · 3.19 KB
/
Parent.swift
File metadata and controls
110 lines (85 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// User.swift
// CP
//
// Created by Gavin Olsen on 5/8/17.
// Copyright © 2017 Gavin Olsen. All rights reserved.
//
/*****************************************
the user needs an array of children, and an
array of carpools.
This will be comparable to teh
*****************************************/
import Foundation
import CloudKit
class Parent: CloudKitSync {
//MARK: keys
static let typeKey = "Parent"
static let nameKey = "nameKey"
static let kidKey = "kidKey"
static let carpoolKey = "carpoolkey"
static let carpoolDictKey = "carpoolDictKey"
static let isLeaderKey = "leader????"
static let userRecordIDKey = "userRecordIDKey"
//MARK: properties
var name: String
var kids: [Child] = [] {
didSet {
DispatchQueue.main.async {
let nc = NotificationCenter.default
nc.post(name: ParentController.ChildArrayNotification, object: self)
}}}
var carpools: [Carpool] = [] {
didSet {
DispatchQueue.main.async {
let nc = NotificationCenter.default
nc.post(name: ParentController.CarpoolArrayNotification, object: self)
}}}
var leaderdCarpools: [Carpool] = []
var isLeader: Bool
var recordType: String { return Parent.typeKey }
var ckRecordID: CKRecordID?
let ckManager = CloudKitManager()
//MARK: initilizers
init(name: String, kids: [Child] = [], carpools: [Carpool] = [], isLeader: Bool = false, userRecordID: CKRecordID?) {
self.name = name
self.carpools = carpools
self.kids = []
self.isLeader = isLeader
self.ckRecordID = userRecordID
}
convenience required init?(record: CKRecord) {
guard let name = record[Parent.nameKey] as? String else { print("one of the values from the required init came back as nil"); return nil }
//it would probably be best to get the
//kids here, and add them to the parent...
self.init(name: name, userRecordID: record.recordID)
ckRecordID = record.recordID
}
func getKids(predicate: NSPredicate) -> [Child] {
var kidos: [Child] = []
CloudKitManager.shared.fetchRecordsWithType(Child.typeKey, predicate: predicate, recordFetchedBlock: nil) { (records, error) in
if error != nil {
print("there's an error: \(String(describing: error))")
}
guard let records = records else { return }
for record in records {
guard let myChild = Child(record: record) else { print("I couldn't get the child back with the record provided"); return }
kidos.append(myChild)
}
}
return kidos
}
}
//MARK: extensions
extension Parent: SearchableRecord {
func matches(searchTerm: String) -> Bool {
return name.contains(searchTerm)
}
}
extension CKRecord {
convenience init(_ parent: Parent) {
let record = CKRecordID(recordName: UUID().uuidString)
self.init(recordType: parent.recordType, recordID: record)
self[Parent.nameKey] = parent.name as CKRecordValue?
}
}