Skip to content

Commit

Permalink
Reduce dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Oct 9, 2016
1 parent 622effc commit 60c60df
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 65 deletions.
84 changes: 64 additions & 20 deletions Demo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

33 changes: 6 additions & 27 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
PODS:
- DATAFilter (0.11.2):
- DATAObjectIDs (~> 0.6.0)
- DATAObjectIDs (0.6.1)
- DATASource (5.10.0)
- DATAStack (5.4.2)
- DateParser (0.1.1)
- JSON (4.0.2)
- NSDictionary-ANDYSafeValue (0.3.1)
- NSEntityDescription-SYNCPrimaryKey (1.2.8):
- NSString-HYPNetworking (~> 1.0.6)
- NSManagedObject-HYPPropertyMapper (4.1.3):
- DateParser (~> 0.1.1)
- NSEntityDescription-SYNCPrimaryKey (~> 1.2.8)
- NSString-HYPNetworking (1.0.6)
- Sync (1.14.4):
- DATAFilter (~> 0.11.2)
- DATAStack (~> 5.4.1)
- NSDictionary-ANDYSafeValue (~> 0.3.1)
- NSManagedObject-HYPPropertyMapper (~> 4.1.1)
- TestCheck (~> 0.3.1)
- TestCheck (0.3.1)
- NSManagedObject-HYPPropertyMapper (4.1.4)
- Sync (1.15.0):
- DATAStack (~> 5.4.2)
- NSManagedObject-HYPPropertyMapper (~> 4.1.4)

DEPENDENCIES:
- DATASource (~> 5)
Expand All @@ -31,18 +17,11 @@ EXTERNAL SOURCES:
:path: "."

SPEC CHECKSUMS:
DATAFilter: 1d339e27caf0a72d895a1ae664e044fff368a520
DATAObjectIDs: add3013859faf600b073051453bb7f902b479334
DATASource: 444542bc20734886c3fa46f53ec4b1dbee750950
DATAStack: 931ef9a21c6badd0ff5de494a5d49109ba7e8115
DateParser: 17a256f651c5dd754b83aca6ef5827d796a5f292
JSON: d08f22c3e523be050d5d5f40bca43ec02d95b2cc
NSDictionary-ANDYSafeValue: 2d7adf339b6e302d71fec5f1d71ae00aacda993e
NSEntityDescription-SYNCPrimaryKey: 6e0ca0958f7e05a15e2ed9317e5afeb75af6c782
NSManagedObject-HYPPropertyMapper: b054e2b9c8be5bb24a7f6f1729395cdb5903171c
NSString-HYPNetworking: 97eb879c5c43663dde06f89e01e8e1551a3f5bb7
Sync: a41f5628374be4c3f7b7c7f13cdbc7058dc11bcd
TestCheck: 9a9aad0a356703989f4f2b640b6ed95e53e2214f
NSManagedObject-HYPPropertyMapper: 0502d7a0dcb8c0c8112757b80e6318a1baf9c486
Sync: 82e4eb203dc5366aeb57fda516e1666838bbed3a

PODFILE CHECKSUM: 7a3c7e2d1ff850847d49164a7d6f1dedd5261745

Expand Down
87 changes: 87 additions & 0 deletions Source/DATAFilter/DATAFilter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import Foundation
import CoreData

public class DATAFilter: NSObject {
public struct Operation : OptionSetType {
public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public static let Insert = Operation(rawValue: 1 << 0)
public static let Update = Operation(rawValue: 1 << 1)
public static let Delete = Operation(rawValue: 1 << 2)
public static let All: Operation = [.Insert, .Update, .Delete]
}

public class func changes(changes: [[String : AnyObject]],
inEntityNamed entityName: String,
localPrimaryKey: String,
remotePrimaryKey: String,
context: NSManagedObjectContext,
inserted: (JSON: [String : AnyObject]) -> Void,
updated: (JSON: [String : AnyObject], updatedObject: NSManagedObject) -> Void){
self.changes(changes, inEntityNamed: entityName, predicate: nil, operations: .All, localPrimaryKey: localPrimaryKey, remotePrimaryKey: remotePrimaryKey, context: context, inserted: inserted, updated: updated)
}

public class func changes(changes: [[String : AnyObject]],
inEntityNamed entityName: String,
predicate: NSPredicate?,
operations: Operation,
localPrimaryKey: String,
remotePrimaryKey: String,
context: NSManagedObjectContext,
inserted: (JSON: [String : AnyObject]) -> Void,
updated: (JSON: [String : AnyObject], updatedObject: NSManagedObject) -> Void) {
// `DATAObjectIDs.objectIDsInEntityNamed` also deletes all objects that don't have a primary key or that have the same primary key already found in the context
let primaryKeysAndObjectIDs = DATAObjectIDs.objectIDs(inEntityNamed: entityName, withAttributesNamed: localPrimaryKey, context: context, predicate: predicate) as? [NSObject : NSManagedObjectID] ?? [NSObject : NSManagedObjectID]()
let localPrimaryKeys = Array(primaryKeysAndObjectIDs.keys)
let remotePrimaryKeys = changes.map { $0[remotePrimaryKey] }
let remotePrimaryKeysWithoutNils = (remotePrimaryKeys.filter { (($0 as? NSObject) != NSNull()) && ($0 != nil) } as! [NSObject!]) as! [NSObject]

var remotePrimaryKeysAndChanges = [NSObject : [String : AnyObject]]()
for (primaryKey, change) in zip(remotePrimaryKeysWithoutNils, changes) {
remotePrimaryKeysAndChanges[primaryKey] = change
}

var intersection = Set(remotePrimaryKeysWithoutNils)
intersection.intersectInPlace(Set(localPrimaryKeys))
let updatedObjectIDs = Array(intersection)


var deletedObjectIDs = localPrimaryKeys
deletedObjectIDs = deletedObjectIDs.filter { value in
!remotePrimaryKeysWithoutNils.contains { $0.isEqual(value) }
}

var insertedObjectIDs = remotePrimaryKeysWithoutNils
insertedObjectIDs = insertedObjectIDs.filter { value in
!localPrimaryKeys.contains { $0.isEqual(value) }
}

if operations.contains(.Delete) {
for fetchedID in deletedObjectIDs {
let objectID = primaryKeysAndObjectIDs[fetchedID]!
let object = context.objectWithID(objectID)
context.deleteObject(object)
}
}

if operations.contains(.Insert) {
for fetchedID in insertedObjectIDs {
let objectDictionary = remotePrimaryKeysAndChanges[fetchedID]!
inserted(JSON: objectDictionary)
}
}

if operations.contains(.Update) {
for fetchedID in updatedObjectIDs {
let JSON = remotePrimaryKeysAndChanges[fetchedID]!
let objectID = primaryKeysAndObjectIDs[fetchedID]!
let object = context.objectWithID(objectID)
updated(JSON: JSON, updatedObject: object)
}
}
}
}
90 changes: 90 additions & 0 deletions Source/DATAObjectIDs/DATAObjectIDs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import CoreData

public class DATAObjectIDs: NSObject {
public class func objectIDs(inEntityNamed entityName: String, withAttributesNamed attributeName: String, context: NSManagedObjectContext, predicate: NSPredicate?) -> [NSObject: AnyObject] {
return self.generateObjectIDs(inEntityNamed: entityName, withAttributesNamed: attributeName, context: context, predicate: predicate, sortDescriptors: nil)
}

class func generateObjectIDs(inEntityNamed entityName: String, withAttributesNamed attributeName: String, context: NSManagedObjectContext, predicate: NSPredicate?, sortDescriptors: [NSSortDescriptor]?) -> [NSObject: AnyObject] {
var result = [NSObject: AnyObject]()

context.performBlockAndWait {
let expression = NSExpressionDescription()
expression.name = "objectID"
expression.expression = NSExpression.expressionForEvaluatedObject()
expression.expressionResultType = .ObjectIDAttributeType

let request = NSFetchRequest(entityName: entityName)
request.predicate = predicate
request.resultType = .DictionaryResultType
request.propertiesToFetch = [expression, attributeName]
request.sortDescriptors = sortDescriptors

do {
let objects = try context.executeFetchRequest(request)
for object in objects {
let fetchedID = object[attributeName] as! NSObject
let objectID = object["objectID"] as! NSManagedObjectID

if let _ = result[fetchedID] {
context.deleteObject(context.objectWithID(objectID))
} else {
result[fetchedID] = objectID
}
}
} catch let error as NSError {
print("error: \(error)")
}
}

return result
}

class func generateObjectIDs(inEntityNamed entityName: String, context: NSManagedObjectContext, predicate: NSPredicate?, sortDescriptors: [NSSortDescriptor]?) -> [Any] {
var objectIDs = [NSManagedObjectID]()

context.performBlockAndWait {
let request = NSFetchRequest(entityName: entityName)
request.predicate = predicate;
request.resultType = .ManagedObjectIDResultType

do {
objectIDs = try context.executeFetchRequest(request) as? [NSManagedObjectID] ?? [NSManagedObjectID]()
} catch let error as NSError {
print("error: \(error)")
}
}

return objectIDs
}

class func generateAttributes(inEntityNamed entityName: String, attributeName: String, context: NSManagedObjectContext, predicate: NSPredicate?, sortDescriptors: [NSSortDescriptor]?) -> [Any] {
var attributes = [Any]()

context.performBlockAndWait {
let expression = NSExpressionDescription()
expression.name = "objectID"
expression.expression = NSExpression.expressionForEvaluatedObject()
expression.expressionResultType = .ObjectIDAttributeType

let request = NSFetchRequest(entityName: entityName)
request.predicate = predicate
request.resultType = .DictionaryResultType
request.propertiesToFetch = [expression, attributeName]
request.sortDescriptors = sortDescriptors

do {
let objects = try context.executeFetchRequest(request)
for object in objects {
if let fetchedID = object[attributeName] {
attributes.append(fetchedID)
}
}
} catch let error as NSError {
print("error: \(error)")
}
}

return attributes
}
}
6 changes: 2 additions & 4 deletions Source/NSArray+Sync.swift → Source/Sync/NSArray+Sync.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Foundation
import DATAStack
import NSManagedObject_HYPPropertyMapper
import DATAFilter

public extension NSArray {
/**
Expand All @@ -26,9 +25,8 @@ public extension NSArray {

guard let filteredArray = (objectChanges as NSArray).filteredArrayUsingPredicate(predicate) as? [NSManagedObject] else { fatalError("Couldn't cast filteredArray as [NSManagedObject]: \(objectChanges), predicate: \(predicate)") }
for filteredObject in filteredArray {
if let change = filteredObject.hyp_dictionaryUsingRelationshipType(.Array) as? [String : AnyObject] {
filteredChanges.append(change)
}
let change = filteredObject.hyp_dictionaryUsingRelationshipType(.Array)
filteredChanges.append(change)
}
}
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import CoreData
import NSEntityDescription_SYNCPrimaryKey
import DATAStack
import NSString_HYPNetworking
import DATAFilter
import NSManagedObject_HYPPropertyMapper

public extension NSManagedObject {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import CoreData
import NSEntityDescription_SYNCPrimaryKey
import NSString_HYPNetworking

public extension NSManagedObjectContext {
/**
Expand Down
3 changes: 0 additions & 3 deletions Source/Sync.swift → Source/Sync/Sync.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import CoreData
import NSEntityDescription_SYNCPrimaryKey
import DATAFilter
import NSManagedObject_HYPPropertyMapper
import DATAStack
import TestCheck

@objc public class Sync: NSOperation {
var downloadFinished = false
Expand Down
28 changes: 28 additions & 0 deletions Source/TestCheck/TestCheck.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Foundation

@objc public class TestCheck: NSObject {
/**
Method to check wheter your on testing mode or not.
- returns: A Bool, `true` if you're on testing mode, `false` if you're not.
*/
public static let isTesting: Bool = {
let enviroment = NSProcessInfo.processInfo().environment
let serviceName = enviroment["XPC_SERVICE_NAME"]
let injectBundle = enviroment["XCInjectBundle"]
var isRunning = (enviroment["TRAVIS"] != nil || enviroment["XCTestConfigurationFilePath"] != nil)

if !isRunning {
if let serviceName = serviceName {
isRunning = (serviceName as NSString).pathExtension == "xctest"
}
}

if !isRunning {
if let injectBundle = injectBundle {
isRunning = (injectBundle as NSString).pathExtension == "xctest"
}
}

return isRunning
}()
}
9 changes: 3 additions & 6 deletions Sync.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "Sync"
s.version = "1.14.4"
s.version = "1.15.0"
s.summary = "Modern Swift JSON synchronization to Core Data"
s.description = <<-DESC
**Sync** eases your everyday job of parsing a `JSON` response and getting it into Core Data. It uses a convention-over-configuration paradigm to facilitate your workflow.
Expand Down Expand Up @@ -28,9 +28,6 @@ s.source_files = 'Source/**/*'

s.frameworks = 'Foundation', 'CoreData'

s.dependency 'DATAFilter', '~> 0.11.2'
s.dependency 'DATAStack', '~> 5.4.1'
s.dependency 'NSDictionary-ANDYSafeValue', '~> 0.3.1'
s.dependency 'NSManagedObject-HYPPropertyMapper', '~> 4.1.1'
s.dependency 'TestCheck', '~> 0.3.1'
s.dependency 'DATAStack', '~> 5.4.2'
s.dependency 'NSManagedObject-HYPPropertyMapper', '~> 4.1.4'
end

0 comments on commit 60c60df

Please sign in to comment.