simplification

This commit is contained in:
Peter 2015-10-29 19:07:37 -04:00
parent 2c3e97779d
commit d7f6e6cd47
5 changed files with 56 additions and 44 deletions

View File

@ -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 = "<group>"; };
D0CF8A0C1BE2AC1700EC9F12 /* CoreDataExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CoreDataExtensions.swift; sourceTree = "<group>"; };
D0CF8A1F1BE2CA1800EC9F12 /* MeteorCoreDataTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeteorCoreDataTableViewController.swift; sourceTree = "<group>"; };
D0CF8A211BE2D65300EC9F12 /* MeteorCollectionChange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MeteorCollectionChange.swift; sourceTree = "<group>"; };
/* 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 */,

View File

@ -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
}

View File

@ -4,6 +4,6 @@ import Foundation
public class MeteorCoreData {
static let stack:MeteorCoreDataStack = {
print("Initializing MeteorCoreDataStack")
return MeteorCoreDataStack()
return MeteorCoreDataStackPersisted()
}()
}

View File

@ -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)

View File

@ -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()