Skip to content
This repository was archived by the owner on Dec 17, 2018. It is now read-only.
Open
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
2 changes: 2 additions & 0 deletions CloudCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@
D5B2E8B41C3A780C00C0327D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
Expand All @@ -942,6 +943,7 @@
D5B2E8B51C3A780C00C0327D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
APPLICATION_EXTENSION_API_ONLY = YES;
CLANG_ENABLE_MODULES = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
DEFINES_MODULE = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Empty file modified Source/Classes/AsynchronousOperation.swift
100644 → 100755
Empty file.
10 changes: 8 additions & 2 deletions Source/Classes/CloudCore.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ open class CloudCore {
public static var config = CloudCoreConfig()

/// `Tokens` object, read more at class description. By default variable is loaded from User Defaults.
public static var tokens = Tokens.loadFromUserDefaults()
public static var tokens = Tokens.loadFromUserDefaults() {
didSet {
tokens.saveToUserDefaults()
}
}

/// Error and sync actions are reported to that delegate
public static weak var delegate: CloudCoreDelegate? {
Expand Down Expand Up @@ -86,7 +90,9 @@ open class CloudCore {
// Subscribe (subscription may be outdated/removed)
#if !os(watchOS)
let subscribeOperation = SubscribeOperation()
subscribeOperation.errorBlock = { handle(subscriptionError: $0, container: container) }
subscribeOperation.errorBlock = {
handle(subscriptionError: $0, container: container)
}
queue.addOperation(subscribeOperation)
#endif

Expand Down
Empty file modified Source/Classes/ErrorBlockProxy.swift
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions Source/Classes/Fetch/FetchAndSaveOperation.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class FetchAndSaveOperation: Operation {
CloudCore.delegate?.didSyncFromCloud()
}

private func addRecordZoneChangesOperation(recordZoneIDs: [CKRecordZoneID], database: CKDatabase, context: NSManagedObjectContext) {
private func addRecordZoneChangesOperation(recordZoneIDs: [CKRecordZone.ID], database: CKDatabase, context: NSManagedObjectContext) {
if recordZoneIDs.isEmpty { return }

let recordZoneChangesOperation = FetchRecordZoneChangesOperation(from: database, recordZoneIDs: recordZoneIDs, tokens: tokens)
Expand All @@ -87,14 +87,14 @@ public class FetchAndSaveOperation: Operation {
self.queue.addOperation(deleteOperation)
}

recordZoneChangesOperation.errorBlock = { zoneID, error in
self.handle(recordZoneChangesError: error, in: zoneID, database: database, context: context)
recordZoneChangesOperation.errorBlock = { recordZoneID, error in
self.handle(recordZoneChangesError: error, in: recordZoneID, database: database, context: context)
}

queue.addOperation(recordZoneChangesOperation)
}

private func handle(recordZoneChangesError: Error, in zoneId: CKRecordZoneID, database: CKDatabase, context: NSManagedObjectContext) {
private func handle(recordZoneChangesError: Error, in recordZoneID: CKRecordZone.ID, database: CKDatabase, context: NSManagedObjectContext) {
guard let cloudError = recordZoneChangesError as? CKError else {
errorBlock?(recordZoneChangesError)
return
Expand All @@ -111,7 +111,7 @@ public class FetchAndSaveOperation: Operation {

// Our token is expired, we need to refetch everything again
case .changeTokenExpired:
tokens.tokensByRecordZoneID[zoneId] = nil
tokens.tokensByRecordZoneID[recordZoneID] = nil
self.addRecordZoneChangesOperation(recordZoneIDs: [CloudCore.config.zoneID], database: database, context: context)
default: errorBlock?(cloudError)
}
Expand Down
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions Source/Classes/Fetch/SubOperations/DeleteFromCoreDataOperation.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import CloudKit

class DeleteFromCoreDataOperation: Operation {
let parentContext: NSManagedObjectContext
let recordID: CKRecordID
let recordID: CKRecord.ID
var errorBlock: ErrorBlock?

init(parentContext: NSManagedObjectContext, recordID: CKRecordID) {
init(parentContext: NSManagedObjectContext, recordID: CKRecord.ID) {
self.parentContext = parentContext
self.recordID = recordID

Expand Down
30 changes: 13 additions & 17 deletions Source/Classes/Fetch/SubOperations/FetchRecordZoneChangesOperation.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import CloudKit
class FetchRecordZoneChangesOperation: Operation {
// Set on init
let tokens: Tokens
let recordZoneIDs: [CKRecordZoneID]
let recordZoneIDs: [CKRecordZone.ID]
let database: CKDatabase
//

var errorBlock: ((CKRecordZoneID, Error) -> Void)?
var errorBlock: ((CKRecordZone.ID, Error) -> Void)?
var recordChangedBlock: ((CKRecord) -> Void)?
var recordWithIDWasDeletedBlock: ((CKRecordID) -> Void)?
var recordWithIDWasDeletedBlock: ((CKRecord.ID) -> Void)?

private let optionsByRecordZoneID: [CKRecordZoneID: CKFetchRecordZoneChangesOptions]
private let optionsByRecordZoneID: [CKRecordZone.ID: CKFetchRecordZoneChangesOperation.ZoneOptions]
private let fetchQueue = OperationQueue()

init(from database: CKDatabase, recordZoneIDs: [CKRecordZoneID], tokens: Tokens) {
init(from database: CKDatabase, recordZoneIDs: [CKRecordZone.ID], tokens: Tokens) {
self.tokens = tokens
self.database = database
self.recordZoneIDs = recordZoneIDs

var optionsByRecordZoneID = [CKRecordZoneID: CKFetchRecordZoneChangesOptions]()
var optionsByRecordZoneID = [CKRecordZone.ID: CKFetchRecordZoneChangesOperation.ZoneOptions]()
for zoneID in recordZoneIDs {
let options = CKFetchRecordZoneChangesOptions()
let options = CKFetchRecordZoneChangesOperation.ZoneOptions()
options.previousServerChangeToken = self.tokens.tokensByRecordZoneID[zoneID]
optionsByRecordZoneID[zoneID] = options
}
Expand All @@ -49,7 +49,7 @@ class FetchRecordZoneChangesOperation: Operation {
fetchQueue.waitUntilAllOperationsAreFinished()
}

private func makeFetchOperation(optionsByRecordZoneID: [CKRecordZoneID: CKFetchRecordZoneChangesOptions]) -> CKFetchRecordZoneChangesOperation {
private func makeFetchOperation(optionsByRecordZoneID: [CKRecordZone.ID: CKFetchRecordZoneChangesOperation.ZoneOptions]) -> CKFetchRecordZoneChangesOperation {
// Init Fetch Operation
let fetchOperation = CKFetchRecordZoneChangesOperation(recordZoneIDs: recordZoneIDs, optionsByRecordZoneID: optionsByRecordZoneID)

Expand All @@ -59,16 +59,12 @@ class FetchRecordZoneChangesOperation: Operation {
fetchOperation.recordWithIDWasDeletedBlock = { recordID, _ in
self.recordWithIDWasDeletedBlock?(recordID)
}
fetchOperation.recordZoneFetchCompletionBlock = { zoneId, serverChangeToken, clientChangeTokenData, isMore, error in
self.tokens.tokensByRecordZoneID[zoneId] = serverChangeToken

fetchOperation.recordZoneChangeTokensUpdatedBlock = { recordZoneID, serverChangeToken, clientChangeTokenData in
self.tokens.tokensByRecordZoneID[recordZoneID] = serverChangeToken
}
fetchOperation.recordZoneFetchCompletionBlock = { recordZoneID, serverChangeToken, clientChangeTokenData, moreComing, error in
if let error = error {
self.errorBlock?(zoneId, error)
}

if isMore {
let moreOperation = self.makeFetchOperation(optionsByRecordZoneID: optionsByRecordZoneID)
self.fetchQueue.addOperation(moreOperation)
self.errorBlock?(recordZoneID, error)
}
}

Expand Down
Empty file.
Empty file.
4 changes: 2 additions & 2 deletions Source/Classes/Save/CloudSaveOperationQueue.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class CloudSaveOperationQueue: OperationQueue {
}
}

private func addOperation(recordsToSave: [CKRecord], recordIDsToDelete: [CKRecordID], database: CKDatabase) {
private func addOperation(recordsToSave: [CKRecord], recordIDsToDelete: [CKRecord.ID], database: CKDatabase) {
// Modify CKRecord Operation
let modifyOperation = CKModifyRecordsOperation(recordsToSave: recordsToSave, recordIDsToDelete: recordIDsToDelete)
modifyOperation.savePolicy = .changedKeys
Expand Down Expand Up @@ -81,7 +81,7 @@ class CloudSaveOperationQueue: OperationQueue {
fileprivate class DatabaseModifyDataSource {
let database: CKDatabase
var save = [CKRecord]()
var delete = [CKRecordID]()
var delete = [CKRecord.ID]()

init(database: CKDatabase) {
self.database = database
Expand Down
Empty file modified Source/Classes/Save/CoreDataListener.swift
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions Source/Classes/Save/Model/RecordIDWithDatabase.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import CloudKit

class RecordIDWithDatabase {
let recordID: CKRecordID
let recordID: CKRecord.ID
let database: CKDatabase

init(_ recordID: CKRecordID, _ database: CKDatabase) {
init(_ recordID: CKRecord.ID, _ database: CKDatabase) {
self.recordID = recordID
self.database = database
}
Expand Down
Empty file modified Source/Classes/Save/Model/RecordWithDatabase.swift
100644 → 100755
Empty file.
Empty file modified Source/Classes/Save/ObjectToRecord/CoreDataAttribute.swift
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions Source/Classes/Save/ObjectToRecord/CoreDataRelationship.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class CoreDataRelationship {

guard let objectsSet = value as? NSSet else { return nil }

var referenceList = [CKReference]()
var referenceList = [CKRecord.Reference]()
for (_, managedObject) in objectsSet.enumerated() {
guard let managedObject = managedObject as? NSManagedObject,
let reference = try makeReference(from: managedObject) else { continue }
Expand All @@ -64,8 +64,8 @@ class CoreDataRelationship {
}
}

private func makeReference(from managedObject: NSManagedObject) throws -> CKReference? {
let action: CKReferenceAction
private func makeReference(from managedObject: NSManagedObject) throws -> CKRecord.Reference? {
let action: CKRecord_Reference_Action
if case .some(NSDeleteRule.cascadeDeleteRule) = description.inverseRelationship?.deleteRule {
action = .deleteSelf
} else {
Expand All @@ -79,7 +79,7 @@ class CoreDataRelationship {
return nil
}

return CKReference(record: record, action: action)
return CKRecord.Reference(record: record, action: action)
}

}
2 changes: 1 addition & 1 deletion Source/Classes/Save/ObjectToRecord/ObjectToRecordConverter.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ObjectToRecordConverter {
}

/// Get appropriate database for modify operations
private func database(for recordID: CKRecordID, serviceAttributes: ServiceAttributeNames) -> CKDatabase {
private func database(for recordID: CKRecord.ID, serviceAttributes: ServiceAttributeNames) -> CKDatabase {
let container = CloudCore.config.container

if serviceAttributes.isPublic { return container.publicCloudDatabase }
Expand Down
Empty file.
Empty file.
Empty file modified Source/Classes/Setup Operation/SetupOperation.swift
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion Source/Classes/Setup Operation/SubscribeOperation.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SubscribeOperation: AsynchronousOperation {
}

private func makeRecordZoneSubscriptionOperation(for database: CKDatabase, id: String) -> CKModifySubscriptionsOperation {
let notificationInfo = CKNotificationInfo()
let notificationInfo = CKSubscription.NotificationInfo()
notificationInfo.shouldSendContentAvailable = true

let subscription = CKRecordZoneSubscription(zoneID: CloudCore.config.zoneID, subscriptionID: id)
Expand Down
Empty file.
4 changes: 4 additions & 0 deletions Source/Classes/Test.xcdatamodeld/Test.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="14315.2.4" systemVersion="17G65" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<elements/>
</model>
Empty file modified Source/Enum/CloudCoreError.swift
100644 → 100755
Empty file.
Empty file modified Source/Enum/FetchResult.swift
100644 → 100755
Empty file.
Empty file modified Source/Enum/Module.swift
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions Source/Extensions/CKRecordID.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

import CloudKit

extension CKRecordID {
extension CKRecord.ID {
private static let separator = "|"

/// Init from encoded string
///
/// - Parameter encodedString: format: `recordName|ownerName`
convenience init?(encodedString: String) {
let separated = encodedString.components(separatedBy: CKRecordID.separator)
let separated = encodedString.components(separatedBy: CKRecord.ID.separator)

if separated.count == 2 {
let zoneID = CKRecordZoneID(zoneName: CloudCore.config.zoneID.zoneName, ownerName: separated[1])
let zoneID = CKRecordZone.ID(zoneName: CloudCore.config.zoneID.zoneName, ownerName: separated[1])
self.init(recordName: separated[0], zoneID: zoneID)
} else {
return nil
Expand All @@ -27,6 +27,6 @@ extension CKRecordID {

/// Encoded string in format: `recordName|ownerName`
var encodedString: String {
return recordName + CKRecordID.separator + zoneID.ownerName
return recordName + CKRecord.ID.separator + zoneID.ownerName
}
}
Empty file modified Source/Extensions/NSEntityDescription.swift
100644 → 100755
Empty file.
5 changes: 3 additions & 2 deletions Source/Extensions/NSManagedObject.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ extension NSManagedObject {
guard let serviceAttributeNames = self.entity.serviceAttributeNames else {
throw CloudCoreError.missingServiceAttributes(entityName: self.entity.name)
}

let record = CKRecord(recordType: entityName, zoneID: CloudCore.config.zoneID)

let recordID = CKRecord.ID(recordName: entityName, zoneID: CloudCore.config.zoneID)
let record = CKRecord(recordType: entityName, recordID: recordID)
self.setValue(record.encdodedSystemFields, forKey: serviceAttributeNames.recordData)
self.setValue(record.recordID.encodedString, forKey: serviceAttributeNames.recordID)

Expand Down
Empty file modified Source/Extensions/NSManagedObjectModel.swift
100644 → 100755
Empty file.
Empty file modified Source/Model/CKRecord.swift
100644 → 100755
Empty file.
14 changes: 9 additions & 5 deletions Source/Model/CloudCoreConfig.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import CloudKit
*/
public struct CloudCoreConfig {

// MARK: - Init

public init() { }

// MARK: CloudKit

/// The CKContainer to store CoreData. Set this to a custom container to
Expand All @@ -37,15 +41,15 @@ public struct CloudCoreConfig {
/// RecordZone inside private database to store CoreData.
///
/// Default value is `CloudCore`
public var zoneID = CKRecordZoneID(zoneName: "CloudCore", ownerName: CKCurrentUserDefaultName)
let subscriptionIDForPrivateDB = "CloudCorePrivate"
let subscriptionIDForSharedDB = "CloudCoreShared"
public var zoneID = CKRecordZone.ID(zoneName: "CloudCore", ownerName: CKCurrentUserDefaultName)
public var subscriptionIDForPrivateDB = "CloudCorePrivate"
public var subscriptionIDForSharedDB = "CloudCoreShared"

/// subscriptionID's prefix for custom CKSubscription in public databases
var publicSubscriptionIDPrefix = "CloudCore-"
public var publicSubscriptionIDPrefix = "CloudCore-"

// MARK: Core Data
let contextName = "CloudCoreFetchAndSave"
public var contextName = "CloudCoreFetchAndSave"

/// Default entity's attribute name for *Record ID* if User Info is not specified.
///
Expand Down
6 changes: 3 additions & 3 deletions Source/Model/CloudKitAttribute.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class CloudKitAttribute {

func makeCoreDataValue() throws -> Any? {
switch value {
case let reference as CKReference: return try findManagedObject(for: reference.recordID)
case let references as [CKReference]:
case let reference as CKRecord.Reference: return try findManagedObject(for: reference.recordID)
case let references as [CKRecord.Reference]:
let managedObjects = NSMutableSet()
for ref in references {
guard let foundObject = try findManagedObject(for: ref.recordID) else { continue }
Expand All @@ -45,7 +45,7 @@ class CloudKitAttribute {
}
}

private func findManagedObject(for recordID: CKRecordID) throws -> NSManagedObject? {
private func findManagedObject(for recordID: CKRecord.ID) throws -> NSManagedObject? {
let targetEntityName = try findTargetEntityName()
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: targetEntityName)

Expand Down
Empty file modified Source/Model/ServiceAttributeName.swift
100644 → 100755
Empty file.
8 changes: 4 additions & 4 deletions Source/Model/Tokens.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import CloudKit
*/
open class Tokens: NSObject, NSCoding {

var tokensByRecordZoneID = [CKRecordZoneID: CKServerChangeToken]()
var tokensByRecordZoneID = [CKRecordZone.ID: CKServerChangeToken]()

private struct ArchiverKey {
static let tokensByRecordZoneID = "tokensByRecordZoneID"
Expand All @@ -45,7 +45,7 @@ open class Tokens: NSObject, NSCoding {
/// Load saved Tokens from UserDefaults. Key is used from `CloudCoreConfig.userDefaultsKeyTokens`
///
/// - Returns: previously saved `Token` object, if tokens weren't saved before newly initialized `Tokens` object will be returned
open static func loadFromUserDefaults() -> Tokens {
public static func loadFromUserDefaults() -> Tokens {
guard let tokensData = UserDefaults.standard.data(forKey: CloudCore.config.userDefaultsKeyTokens),
let tokens = NSKeyedUnarchiver.unarchiveObject(with: tokensData) as? Tokens else {
return Tokens()
Expand All @@ -56,16 +56,16 @@ open class Tokens: NSObject, NSCoding {

/// Save tokens to UserDefaults and synchronize. Key is used from `CloudCoreConfig.userDefaultsKeyTokens`
open func saveToUserDefaults() {

let tokensData = NSKeyedArchiver.archivedData(withRootObject: self)
UserDefaults.standard.set(tokensData, forKey: CloudCore.config.userDefaultsKeyTokens)
UserDefaults.standard.synchronize()
}

// MARK: NSCoding

/// Returns an object initialized from data in a given unarchiver.
public required init?(coder aDecoder: NSCoder) {
if let decodedTokens = aDecoder.decodeObject(forKey: ArchiverKey.tokensByRecordZoneID) as? [CKRecordZoneID: CKServerChangeToken] {
if let decodedTokens = aDecoder.decodeObject(forKey: ArchiverKey.tokensByRecordZoneID) as? [CKRecordZone.ID: CKServerChangeToken] {
self.tokensByRecordZoneID = decodedTokens
} else {
return nil
Expand Down
Empty file modified Source/Protocols/CloudCoreDelegate.swift
100644 → 100755
Empty file.