Change to use 0 and 1 for boolean.

This commit is contained in:
Maurice Parker 2020-03-31 20:42:39 -05:00
parent 3f82a28d21
commit c3b5d337c5
3 changed files with 11 additions and 5 deletions

View File

@ -55,6 +55,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) { func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
let group = DispatchGroup() let group = DispatchGroup()
BatchUpdate.shared.start()
zones.forEach { zone in zones.forEach { zone in
group.enter() group.enter()
@ -64,6 +65,7 @@ final class CloudKitAccountDelegate: AccountDelegate {
} }
group.notify(queue: DispatchQueue.main) { group.notify(queue: DispatchQueue.main) {
BatchUpdate.shared.end()
completion() completion()
} }
} }

View File

@ -198,13 +198,17 @@ final class CloudKitAccountZone: CloudKitZone {
} }
func findOrCreateAccount(completion: @escaping (Result<String, Error>) -> Void) { func findOrCreateAccount(completion: @escaping (Result<String, Error>) -> Void) {
let predicate = NSPredicate(format: "isAccount = \"true\"") let predicate = NSPredicate(format: "isAccount = \"1\"")
let ckQuery = CKQuery(recordType: CloudKitContainer.recordType, predicate: predicate) let ckQuery = CKQuery(recordType: CloudKitContainer.recordType, predicate: predicate)
query(ckQuery) { result in query(ckQuery) { result in
switch result { switch result {
case .success(let records): case .success(let records):
completion(.success(records[0].externalID)) if records.count > 0 {
completion(.success(records[0].externalID))
} else {
self.createContainer(name: "Account", isAccount: true, completion: completion)
}
case .failure: case .failure:
self.createContainer(name: "Account", isAccount: true, completion: completion) self.createContainer(name: "Account", isAccount: true, completion: completion)
} }
@ -256,14 +260,14 @@ private extension CloudKitAccountZone {
func newContainerCKRecord(name: String) -> CKRecord { func newContainerCKRecord(name: String) -> CKRecord {
let record = CKRecord(recordType: CloudKitContainer.recordType, recordID: generateRecordID()) let record = CKRecord(recordType: CloudKitContainer.recordType, recordID: generateRecordID())
record[CloudKitContainer.Fields.name] = name record[CloudKitContainer.Fields.name] = name
record[CloudKitContainer.Fields.isAccount] = "false" record[CloudKitContainer.Fields.isAccount] = "0"
return record return record
} }
func createContainer(name: String, isAccount: Bool, completion: @escaping (Result<String, Error>) -> Void) { func createContainer(name: String, isAccount: Bool, completion: @escaping (Result<String, Error>) -> Void) {
let record = CKRecord(recordType: CloudKitContainer.recordType, recordID: generateRecordID()) let record = CKRecord(recordType: CloudKitContainer.recordType, recordID: generateRecordID())
record[CloudKitContainer.Fields.name] = name record[CloudKitContainer.Fields.name] = name
record[CloudKitContainer.Fields.isAccount] = isAccount ? "true" : "false" record[CloudKitContainer.Fields.isAccount] = isAccount ? "1" : "0"
save(record) { result in save(record) { result in
switch result { switch result {

View File

@ -83,7 +83,7 @@ class CloudKitAcountZoneDelegate: CloudKitZoneDelegate {
guard let account = account, guard let account = account,
let name = record[CloudKitAccountZone.CloudKitContainer.Fields.name] as? String, let name = record[CloudKitAccountZone.CloudKitContainer.Fields.name] as? String,
let isAccount = record[CloudKitAccountZone.CloudKitContainer.Fields.isAccount] as? String, let isAccount = record[CloudKitAccountZone.CloudKitContainer.Fields.isAccount] as? String,
isAccount != "true" else { return } isAccount != "1" else { return }
var folder = account.existingFolder(withExternalID: record.externalID) var folder = account.existingFolder(withExternalID: record.externalID)
folder?.name = name folder?.name = name