diff --git a/SwiftDDP.xcodeproj/project.pbxproj b/SwiftDDP.xcodeproj/project.pbxproj index 0c1d831..4d72e49 100644 --- a/SwiftDDP.xcodeproj/project.pbxproj +++ b/SwiftDDP.xcodeproj/project.pbxproj @@ -32,6 +32,7 @@ D0CF8A0F1BE2AC1700EC9F12 /* MeteorCoreDataCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0CF8A0B1BE2AC1700EC9F12 /* MeteorCoreDataCollection.swift */; settings = {ASSET_TAGS = (); }; }; D0CF8A101BE2AC1700EC9F12 /* CoreDataExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0CF8A0C1BE2AC1700EC9F12 /* CoreDataExtensions.swift */; settings = {ASSET_TAGS = (); }; }; D0CF8A201BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0CF8A1F1BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift */; settings = {ASSET_TAGS = (); }; }; + D0CF8A221BE2D65300EC9F12 /* MeteorCollectionChange.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0CF8A211BE2D65300EC9F12 /* MeteorCollectionChange.swift */; settings = {ASSET_TAGS = (); }; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -95,6 +96,7 @@ D0CF8A0B1BE2AC1700EC9F12 /* MeteorCoreDataCollection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeteorCoreDataCollection.swift; sourceTree = ""; }; D0CF8A0C1BE2AC1700EC9F12 /* CoreDataExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataExtensions.swift; sourceTree = ""; }; D0CF8A1F1BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeteorCoreDataTableViewController.swift; sourceTree = ""; }; + D0CF8A211BE2D65300EC9F12 /* MeteorCollectionChange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeteorCollectionChange.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -193,6 +195,7 @@ D0CF8A0A1BE2AC1700EC9F12 /* MeteorCoreDataStack.swift */, D0CF8A0B1BE2AC1700EC9F12 /* MeteorCoreDataCollection.swift */, D0CF8A1F1BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift */, + D0CF8A211BE2D65300EC9F12 /* MeteorCollectionChange.swift */, D0CF8A0C1BE2AC1700EC9F12 /* CoreDataExtensions.swift */, ); name = MeteorCoreData; @@ -364,6 +367,7 @@ buildActionMask = 2147483647; files = ( D0CF8A201BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift in Sources */, + D0CF8A221BE2D65300EC9F12 /* MeteorCollectionChange.swift in Sources */, D0CF8A0F1BE2AC1700EC9F12 /* MeteorCoreDataCollection.swift in Sources */, D0CF8A101BE2AC1700EC9F12 /* CoreDataExtensions.swift in Sources */, D02A72101BBEFCD300940C17 /* DDPClient.swift in Sources */, diff --git a/SwiftDDP/MeteorCollectionChange.swift b/SwiftDDP/MeteorCollectionChange.swift new file mode 100644 index 0000000..84f99aa --- /dev/null +++ b/SwiftDDP/MeteorCollectionChange.swift @@ -0,0 +1,35 @@ + +import Foundation + +// +// MeteorCollectionChange +// +// + +struct MeteorCollectionChange: Hashable { + var id:String + var collection:String + var fields:NSDictionary? + var cleared:[String]? + var hashValue:Int { + var hash = "\(id.hashValue)\(collection.hashValue)" + if let _ = fields { hash += "\(fields!.hashValue)" } + if let _ = cleared { + for value in cleared! { + hash += "\(value.hashValue)" + } + } + return hash.hashValue + } + + init(id:String, collection:String, fields:NSDictionary?, cleared:[String]?){ + self.id = id + self.collection = collection + self.fields = fields + self.cleared = cleared + } +} + +func ==(lhs:MeteorCollectionChange, rhs:MeteorCollectionChange) -> Bool { + return lhs.hashValue == rhs.hashValue +} \ No newline at end of file diff --git a/SwiftDDP/MeteorCoreData.swift b/SwiftDDP/MeteorCoreData.swift index 6d68694..acc52d0 100644 --- a/SwiftDDP/MeteorCoreData.swift +++ b/SwiftDDP/MeteorCoreData.swift @@ -4,6 +4,6 @@ import Foundation public class MeteorCoreData { static let stack:MeteorCoreDataStack = { print("Initializing MeteorCoreDataStack") - return MeteorCoreDataStack() + return MeteorCoreDataStackPersisted() }() } \ No newline at end of file diff --git a/SwiftDDP/MeteorCoreDataCollection.swift b/SwiftDDP/MeteorCoreDataCollection.swift index 61d5b5f..fad7167 100644 --- a/SwiftDDP/MeteorCoreDataCollection.swift +++ b/SwiftDDP/MeteorCoreDataCollection.swift @@ -8,50 +8,10 @@ public protocol MeteorCoreDataCollectionDelegate { func document(willBeUpdatedWith fields:NSDictionary?, cleared:[String]?, forObject object:NSManagedObject) -> NSManagedObject } -// -// -// MeteorCollectionChange -// -// - -struct MeteorCollectionChange: Hashable { - var id:String - var collection:String - var fields:NSDictionary? - var cleared:[String]? - var hashValue:Int { - var hash = "\(id.hashValue)\(collection.hashValue)" - if let _ = fields { hash += "\(fields!.hashValue)" } - if let _ = cleared { - for value in cleared! { - hash += "\(value.hashValue)" - } - } - return hash.hashValue - } - - init(id:String, collection:String, fields:NSDictionary?, cleared:[String]?){ - self.id = id - self.collection = collection - self.fields = fields - self.cleared = cleared - } -} - -func ==(lhs:MeteorCollectionChange, rhs:MeteorCollectionChange) -> Bool { - return lhs.hashValue == rhs.hashValue -} - -// -// -// End Meteor Collection Change -// -// - public class MeteorCoreDataCollection:Collection { private let entityName:String - private let stack = MeteorCoreData.stack + private let stack:MeteorCoreDataCollectionStack private var changeLog = [Int:MeteorCollectionChange]() private lazy var mainContext:NSManagedObjectContext = { return self.stack.mainContext }() @@ -61,6 +21,13 @@ public class MeteorCoreDataCollection:Collection { public init(collectionName:String, entityName:String) { self.entityName = entityName + self.stack = MeteorCoreData.stack + super.init(name: collectionName) + } + + public init(collectionName:String, entityName:String, inMemory:Bool) { + self.entityName = entityName + self.stack = MeteorCoreData.stack super.init(name: collectionName) } @@ -250,7 +217,7 @@ public class MeteorCoreDataCollection:Collection { - override public func documentWasChanged(collection:String, id:String, fields:NSDictionary?, cleared:[String]?) { + override public func documentWasChanged(collection:String, id:String, fields:NSDictionary?, cleared:[String]?) { backgroundContext.performBlock() { let currentChange = MeteorCollectionChange(id: id, collection: collection, fields: fields, cleared: cleared) diff --git a/SwiftDDP/MeteorCoreDataStack.swift b/SwiftDDP/MeteorCoreDataStack.swift index c832e19..2279335 100644 --- a/SwiftDDP/MeteorCoreDataStack.swift +++ b/SwiftDDP/MeteorCoreDataStack.swift @@ -2,7 +2,13 @@ import Foundation import CoreData -public class MeteorCoreDataStack:NSObject { +public protocol MeteorCoreDataCollectionStack { + var mainContext: NSManagedObjectContext { get } + var backgroundContext: NSManagedObjectContext { get } + var managedObjectContext:NSManagedObjectContext { get } +} + +class MeteorCoreDataStackPersisted:NSObject, MeteorCoreDataCollectionStack { override init() { super.init()