Created
October 21, 2016 03:06
-
-
Save dornad/8eabd03fca33b559f00815f40e38d4e7 to your computer and use it in GitHub Desktop.
Core Data Playground (WIP)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/Playgrounds/PricingAPIModels.playground/Contents.swift b/Playgrounds/PricingAPIModels.playground/Contents.swift | |
index f0c121a..2ada632 100644 | |
--- a/Playgrounds/PricingAPIModels.playground/Contents.swift | |
+++ b/Playgrounds/PricingAPIModels.playground/Contents.swift | |
@@ -1,42 +1,50 @@ | |
//: Playground - noun: a place where people can play | |
import UIKit | |
+import CoreData | |
import PPNetworking | |
+import FeatureKit | |
-struct PPFeature: PPNetworkingMappableObject { | |
- let name: String | |
- let coins: Int | |
- let count: Int | |
- | |
- func JSONMapping() -> [AnyObject!: AnyObject] { | |
- return [ | |
- "name": "name", | |
- "coins": "coins", | |
- "count": "count" | |
- ] | |
- } | |
-} | |
-struct PPPricedFeatures { | |
- let features: [PPFeature] | |
- | |
- // TODO: Implementation of adding multiple features | |
+ | |
+let PPFeatureUpcomingEventsReceiver = PPFeature() | |
+ | |
+func PPFeatureIsEnabled(feature: PPFeature) -> Bool { | |
+ return true | |
} | |
-class PPCardPricing: PPNetworkingMappableObject { | |
- let coins: Int | |
- let coinsPer: Int | |
- let features: [PPPricedFeature] | |
+class PostboxRecordPredicates { | |
- func JSONMapping() -> [AnyObject!: AnyObject] { | |
- return [ | |
- "coins": "coins", | |
- "coins_per": "coinsPer", | |
- "features": "features" | |
- ] | |
+ class func upcomingEventsSentAndReceived() -> NSPredicate { | |
+ | |
+ return NSPredicate(format: "%K == %@", "isSentRecord", true as CVarArg) | |
} | |
- func JSONMappingRleationships() -> [AnyObject] { | |
- PPNetworkingRelationship | |
+ class func upcomingSentRSVPEvents() -> NSPredicate { | |
+ | |
+ return upcomingEventsSentAndReceived() // DONT DO THIS, THIS IS HERE JUST TO SATISFY THE COMPILER | |
} | |
} | |
+ | |
+class PostboxRecordFetch { | |
+ | |
+ class func fetchRequestForUpcomingEvents(limit: Int? = nil) -> NSFetchRequest<PPostboxRecord> { | |
+ | |
+ let sortDescriptor = NSSortDescriptor(key: "eventDetails.eventTimeDate", ascending: true) | |
+ | |
+ let fetchRequest: NSFetchRequest<NSFetchRequestResult> = PPostboxRecord.fetchRequest() | |
+ | |
+ if PPFeatureIsEnabled(PPFeatureUpcomingEventsReceiver) { | |
+ fetchRequest.predicate = PostboxRecordPredicates.upcomingEventsSentAndReceived() | |
+ } else { | |
+ fetchRequest.predicate = PostboxRecordPredicates.upcomingSentRSVPEvents() | |
+ } | |
+ | |
+ fetchRequest.fetchLimit = limit ?? 0 | |
+ fetchRequest.sortDescriptors = [sortDescriptor] | |
+ | |
+ return fetchRequest as! NSFetchRequest<PPostboxRecord> | |
+ } | |
+} | |
+ | |
+ | |
diff --git a/Playgrounds/PricingAPIModels.playground/Sources/SupportCode.swift b/Playgrounds/PricingAPIModels.playground/Sources/SupportCode.swift | |
index 767f79e..402f439 100644 | |
--- a/Playgrounds/PricingAPIModels.playground/Sources/SupportCode.swift | |
+++ b/Playgrounds/PricingAPIModels.playground/Sources/SupportCode.swift | |
@@ -1,3 +1,82 @@ | |
// | |
// This file (and all other Swift source files in the Sources directory of this playground) will be precompiled into a framework which is automatically made available to PricingAPIModels.playground. | |
// | |
+ | |
+import CoreData | |
+ | |
+public class PPEventDetails: NSManagedObject { | |
+ @NSManaged public var id: NSNumber | |
+ | |
+} | |
+ | |
+public class PPostboxRecord: NSManagedObject { | |
+ | |
+ @NSManaged public var id: NSNumber | |
+ @NSManaged public var isSentRecord: Bool | |
+ @NSManaged public var eventDetails: PPEventDetails | |
+} | |
+ | |
+ | |
+public class CoreDataStack { | |
+ // 2 | |
+ public let model:NSManagedObjectModel | |
+ public let persistentStoreCoordinator:NSPersistentStoreCoordinator | |
+ public let context:NSManagedObjectContext | |
+ | |
+ public init() { | |
+ | |
+ // 3 | |
+ // initialize our postbox record entity | |
+ let commentEntity = NSEntityDescription() | |
+ commentEntity.name = "PPostboxRecord" | |
+ commentEntity.managedObjectClassName = NSStringFromClass(PPostboxRecord.self) | |
+ | |
+ // create the attributes for the entity | |
+ let idAttribute = NSAttributeDescription() | |
+ idAttribute.name = "id" | |
+ idAttribute.attributeType = .integer64AttributeType | |
+ idAttribute.isOptional = false | |
+ idAttribute.isIndexed = true | |
+ | |
+ let commentAttribute = NSAttributeDescription() | |
+ commentAttribute.name = "isSentRecord" | |
+ commentAttribute.attributeType = .integer64AttributeType | |
+ commentAttribute.isOptional = false | |
+ commentAttribute.isIndexed = false | |
+ | |
+ // add the properties to the entity | |
+ commentEntity.properties = [idAttribute, commentAttribute] | |
+ | |
+ // add the entities to the model | |
+ model = NSManagedObjectModel() | |
+ model.entities = [commentEntity] | |
+ // 4 | |
+ // setup the persistent store coordinator | |
+ persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: model) | |
+ do { | |
+ try persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: nil) | |
+ } | |
+ catch { | |
+ print("error creating persistentstorecoordinator: \(error)") | |
+ } | |
+ // 5 | |
+ // set up managed object context | |
+ context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) | |
+ context.persistentStoreCoordinator = persistentStoreCoordinator | |
+ | |
+ | |
+ } | |
+ // 6 | |
+ public func saveContext () { | |
+ if context.hasChanges { | |
+ do { | |
+ try context.save() | |
+ } catch { | |
+ let nserror = error as NSError | |
+ NSLog("Unresolved error \(nserror), \(nserror.userInfo)") | |
+ abort() | |
+ } | |
+ } | |
+ } | |
+ | |
+} | |
diff --git a/Playgrounds/PricingAPIModels.playground/timeline.xctimeline b/Playgrounds/PricingAPIModels.playground/timeline.xctimeline | |
deleted file mode 100644 | |
index bf468af..0000000 | |
--- a/Playgrounds/PricingAPIModels.playground/timeline.xctimeline | |
+++ /dev/null | |
@@ -1,6 +0,0 @@ | |
-<?xml version="1.0" encoding="UTF-8"?> | |
-<Timeline | |
- version = "3.0"> | |
- <TimelineItems> | |
- </TimelineItems> | |
-</Timeline> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment