Skip to content

Commit

Permalink
Can now use sub objects
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Dec 24, 2015
1 parent 1d4bb7e commit be628bd
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 12 deletions.
4 changes: 4 additions & 0 deletions AppMessage/AppMessage.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
7FCB04A419A8E14F00A257FE /* News.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FCB04A319A8E14F00A257FE /* News.swift */; };
7FD051C119C591B2009B7FEE /* image-not-available.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 7FD051C019C591B2009B7FEE /* image-not-available.jpg */; };
7FD41611199666F3003C2CE9 /* EVCloudData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD41610199666F3003C2CE9 /* EVCloudData.swift */; };
7FD67D981C2BD8B40071234B /* Invoice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FD67D971C2BD8B40071234B /* Invoice.swift */; };
7FEC7BFD1AEA5EE50004F81D /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 7FEC7BFC1AEA5EE50004F81D /* LaunchScreen.xib */; };
7FF11C411ACBEA6000A18FA3 /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FF11C401ACBEA6000A18FA3 /* SearchViewController.swift */; };
7FF447441A16964C0003AE91 /* ChatViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7FF447431A16964C0003AE91 /* ChatViewController.swift */; };
Expand Down Expand Up @@ -110,6 +111,7 @@
7FCB04A319A8E14F00A257FE /* News.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = News.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
7FD051C019C591B2009B7FEE /* image-not-available.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "image-not-available.jpg"; sourceTree = "<group>"; };
7FD41610199666F3003C2CE9 /* EVCloudData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = EVCloudData.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
7FD67D971C2BD8B40071234B /* Invoice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Invoice.swift; sourceTree = "<group>"; };
7FEC7BFC1AEA5EE50004F81D /* LaunchScreen.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LaunchScreen.xib; sourceTree = "<group>"; };
7FED8DF91A4A30B70071F7A9 /* EVCloudKitDao.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; name = EVCloudKitDao.podspec; path = ../EVCloudKitDao.podspec; sourceTree = "<group>"; };
7FED8DFA1A4A30C10071F7A9 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
Expand Down Expand Up @@ -254,6 +256,7 @@
7F434A4B1962AE780081F35C /* Message.swift */,
7F76554B197FF0FE008AD0D5 /* Asset.swift */,
7FCB04A319A8E14F00A257FE /* News.swift */,
7FD67D971C2BD8B40071234B /* Invoice.swift */,
);
path = DataObjects;
sourceTree = "<group>";
Expand Down Expand Up @@ -661,6 +664,7 @@
7FC46F9019824285000A875F /* HomeViewController.swift in Sources */,
7FCB04A419A8E14F00A257FE /* News.swift in Sources */,
7FC46F981982805A000A875F /* SettingsViewController.swift in Sources */,
7FD67D981C2BD8B40071234B /* Invoice.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 4 additions & 0 deletions AppMessage/AppMessage/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// To be sure you do not have any old subscriptions left over, just clear them all on startup.
// EVCloudKitDao.publicDB.unsubscribeAll({subscriptioncount in EVLog("subscriptions removed = \(subscriptioncount)")}, errorHandler: {error in })

EVCloudKitDao.publicDB.unsubscribeAll( { count in
EVLog("\(count) subscriptions removed")
})

return true
}

Expand Down
47 changes: 40 additions & 7 deletions AppMessage/AppMessage/CloudKit/EVCloudKitDao.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
// Copyright (c) 2014 EVICT BV. All rights reserved.
//

import Foundation
import CloudKit
import EVReflection



/**
Class for simplified access to Apple's CloudKit data where you still have full control
*/
Expand Down Expand Up @@ -841,6 +843,7 @@ public class EVCloudKitDao {
}
} else {
executeIfNonQuery()
EVLog("WARNING: The retrieved notification is not a CloudKit query notification.\n===>userInfo = \(userInfo)\nnotification = \(cloudNotification)")
}
fetchChangeNotifications(recordID, inserted: inserted , updated: updated, deleted: deleted, completed:completed)
}
Expand Down Expand Up @@ -989,18 +992,32 @@ public class EVCloudKitDao {
record = CKRecord(recordType: EVReflection.swiftStringFromClass(theObject), recordID: theObject.recordID)
}
let (fromDict, _) = EVReflection.toDictionary(theObject)
for (key, value) in fromDict {
dictToCKRecord(record, dict: fromDict)

return record
}

/**
Put a dictionary recursively in a CKRecord
- parameter record: the record
- parameter dict: the dictionary
- parameter root: used for expanding the property name
*/
private func dictToCKRecord(record: CKRecord, dict: NSDictionary, root:String = "") {
for (key, value) in dict {
if !(["recordID", "recordType", "creationDate", "creatorUserRecordID", "modificationDate", "lastModifiedUserRecordID", "recordChangeTag", "encodedSystemFields"]).contains(key as! String) {
if let _ = value as? NSNull {
// record.setValue(nil, forKey: key) // Swift can not set a value on a nulable type.
if value is NSNull {
// record.setValue(nil, forKey: key) // Swift can not set a value on a nulable type.
} else if let dict = value as? NSDictionary {
dictToCKRecord(record, dict: dict, root: "\(root)\(key as! String)__")
} else if key as! String != "recordID" {
record.setValue(value, forKey: key as! String)
record.setValue(value, forKey: "\(root)\(key as! String)")
}
}
}
return record
}

/**
Convert CKRecord to dictionary
Expand All @@ -1011,7 +1028,23 @@ public class EVCloudKitDao {
let dictionary = NSMutableDictionary()
for key in record.allKeys() {
if let value = record.objectForKey(key) {
dictionary.setObject(value, forKey: key)
var path:[String] = key.componentsSeparatedByString("__")
if path.count == 1 {
dictionary.setObject(value, forKey: key)
} else {
var tempDict = dictionary
var tempKey = key
let lastKey = path[path.count - 1]
path.removeLast()
for item in path {
tempKey = item
if tempDict[tempKey] == nil {
tempDict.setObject(NSMutableDictionary(), forKey: tempKey)
}
tempDict = (tempDict[tempKey] as? NSMutableDictionary) ?? NSMutableDictionary()
}
tempDict.setObject(value, forKey: lastKey)
}
}
}
return dictionary
Expand Down
37 changes: 37 additions & 0 deletions AppMessage/AppMessage/Controlers/TestsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class TestsViewController: UIViewController {
@IBAction func runTest(sender: AnyObject) {
getUserInfoTest() // will set the self.userId

subObjectTest()

removeAllSubscriptionsTest()

getAllContactsTest()
Expand Down Expand Up @@ -262,4 +264,39 @@ class TestsViewController: UIViewController {
})
}

func subObjectTest() {
let invoice = Invoice()
invoice.InvoiceNumber = "A123"
invoice.DeliveryAddress = Address()
invoice.DeliveryAddress?.Street = "The street"
invoice.DeliveryAddress?.City = "The city"
invoice.InvoiceAddress = Address()
invoice.InvoiceAddress?.Street = "The invoice street"
invoice.InvoiceAddress?.City = "The invoice city"
invoice.PostalAddress = Address()
invoice.PostalAddress?.Street = "The postal street"
invoice.PostalAddress?.City = "The postal city"

// Save the invoice and wait for it to complete
let sema = dispatch_semaphore_create(0);
self.dao.saveItem(invoice, completionHandler: {record in
EVLog("saveItem Invoice: \(record.recordID.recordName)");
dispatch_semaphore_signal(sema);
}, errorHandler: {error in
EVLog("<--- ERROR saveItem message");
dispatch_semaphore_signal(sema);
})
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);


// Now see if we can query the invoice
// Get all records of a recordType
dao.query(Invoice(), completionHandler: { results, isFinished in
EVLog("query Invoice : result count = \(results.count), results = \(results)")
return false
}, errorHandler: { error in
EVLog("<--- ERROR query Invoice")
})
}

}
25 changes: 25 additions & 0 deletions AppMessage/AppMessage/DataObjects/Invoice.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Invoice.swift
// AppMessage
//
// Created by Edwin Vermeer on 12/24/15.
// Copyright © 2015 mirabeau. All rights reserved.
//

import Foundation

class Invoice: EVCloudKitDataObject {
var InvoiceNumber: String?
var InvoiceAddress: Address?
var DeliveryAddress: Address?
var PostalAddress: Address?
}

class Address: EVCloudKitDataObject {
var Street: String = ""
var HouseNumber: String = ""
var PostalCode: String = ""
var City: String = ""
var State: String = ""
var Country: String = ""
}
5 changes: 3 additions & 2 deletions AppMessage/AppMessage/LaunchScreen.xib
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="9531" systemVersion="15C50" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9529"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
Expand Down
4 changes: 2 additions & 2 deletions AppMessage/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- AsyncSwift (1.6.4)
- CRToast (0.0.9)
- EVReflection (2.12.1)
- EVReflection (2.14.1)
- JSQMessagesViewController (7.2.0):
- JSQSystemSoundPlayer (~> 2.0.1)
- JSQSystemSoundPlayer (2.0.1)
Expand Down Expand Up @@ -36,7 +36,7 @@ CHECKOUT OPTIONS:
SPEC CHECKSUMS:
AsyncSwift: 3c90beed237324f1b57e1f1c414dc5501985d208
CRToast: 5a78c22b921c5ed3487488af605bc403a9c92fed
EVReflection: 1b74180614fe32f6fd3b37e32006f0047343f0c7
EVReflection: d018df32f97a41b808cc36738a7b48fad57d86aa
JSQMessagesViewController: 73cab48aa92fc2d512f3b6724f3425cc47a19eb5
JSQSystemSoundPlayer: c5850e77a4363ffd374cd851154b9af93264ed8d
PermissionScope: 1d27791185cca10c26f84d1aab905117dde50f39
Expand Down
2 changes: 1 addition & 1 deletion EVCloudKitDao.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVCloudKitDao"
s.version = "2.14.2"
s.version = "2.15.0"
s.summary = "iOS: Simplified access to Apple’s CloudKit"

s.description = "Simplified access to Apple’s CloudKit using reflection and generics"
Expand Down

0 comments on commit be628bd

Please sign in to comment.