Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba5d3c8
commit aef7a7e
Showing
9 changed files
with
688 additions
and
1,098 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file modified
BIN
+14.7 KB
(290%)
DownTube/DownTube.xcworkspace/xcuserdata/adam.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
...ube/DownTube.xcworkspace/xcuserdata/adam.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Bucket | ||
type = "0" | ||
version = "2.0"> | ||
</Bucket> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// | ||
// CoreDataController.swift | ||
// DownTube | ||
// | ||
// Created by Adam Boyd on 2016-05-30. | ||
// Copyright © 2016 Adam. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
class CoreDataController { | ||
static let sharedController = CoreDataController() | ||
|
||
//MARK: - Core Data Stack | ||
|
||
var fetchedResultsController: NSFetchedResultsController { | ||
if _fetchedResultsController != nil { | ||
return _fetchedResultsController! | ||
} | ||
|
||
let fetchRequest = NSFetchRequest() | ||
// Edit the entity name as appropriate. | ||
let entity = NSEntityDescription.entityForName("Video", inManagedObjectContext: self.managedObjectContext) | ||
fetchRequest.entity = entity | ||
|
||
// Set the batch size to a suitable number. | ||
fetchRequest.fetchBatchSize = 20 | ||
|
||
//Order: most recent first | ||
let sortDescriptor = NSSortDescriptor(key: "created", ascending: false) | ||
|
||
fetchRequest.sortDescriptors = [sortDescriptor] | ||
|
||
// Edit the section name key path and cache name if appropriate. | ||
// nil for section name key path means "no sections". | ||
let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext, sectionNameKeyPath: nil, cacheName: "Master") | ||
_fetchedResultsController = aFetchedResultsController | ||
|
||
do { | ||
try _fetchedResultsController!.performFetch() | ||
} catch { | ||
print("Could not save to Core Data") | ||
} | ||
|
||
return _fetchedResultsController! | ||
} | ||
var _fetchedResultsController: NSFetchedResultsController? = nil | ||
|
||
lazy var applicationDocumentsDirectory: NSURL = { | ||
// The directory the application uses to store the Core Data store file. This code uses a directory named "com.adam.SlackTeam" in the application's documents Application Support directory. | ||
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) | ||
return urls[urls.count-1] | ||
}() | ||
|
||
lazy var managedObjectModel: NSManagedObjectModel = { | ||
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model. | ||
let modelURL = NSBundle.mainBundle().URLForResource("DownTube", withExtension: "momd")! | ||
return NSManagedObjectModel(contentsOfURL: modelURL)! | ||
}() | ||
|
||
lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator = { | ||
// The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail. | ||
// Create the coordinator and store | ||
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel) | ||
let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("SingleViewCoreData.sqlite") | ||
var failureReason = "There was an error creating or loading the application's saved data." | ||
do { | ||
try coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) | ||
} catch { | ||
// Report any error we got. | ||
var dict = [String: AnyObject]() | ||
dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data" | ||
dict[NSLocalizedFailureReasonErrorKey] = failureReason | ||
|
||
dict[NSUnderlyingErrorKey] = error as NSError | ||
let wrappedError = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) | ||
|
||
NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") | ||
abort() //Abort here if something goes really wrong | ||
} | ||
|
||
return coordinator | ||
}() | ||
|
||
lazy var managedObjectContext: NSManagedObjectContext = { | ||
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail. | ||
let coordinator = self.persistentStoreCoordinator | ||
var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType) | ||
managedObjectContext.persistentStoreCoordinator = coordinator | ||
return managedObjectContext | ||
}() | ||
|
||
// MARK: - Core Data Saving support | ||
|
||
func saveContext () { | ||
if managedObjectContext.hasChanges { | ||
do { | ||
try managedObjectContext.save() | ||
} catch { | ||
let nserror = error as NSError | ||
NSLog("Unresolved error \(nserror), \(nserror.userInfo)") | ||
} | ||
} | ||
} | ||
} |
11 changes: 7 additions & 4 deletions
11
DownTube/DownTube/DownTube.xcdatamodeld/DownTube.xcdatamodel/contents
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model name="Test1.xcdatamodel" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" macOSVersion="Automatic" iOSVersion="Automatic"> | ||
<entity name="Event"> | ||
<attribute name="timeStamp" optional="YES" attributeType="Date"/> | ||
<model userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="10174" systemVersion="15F34" minimumToolsVersion="Automatic"> | ||
<entity name="Video" representedClassName=".Video" syncable="YES"> | ||
<attribute name="created" optional="YES" attributeType="Date" syncable="YES"/> | ||
<attribute name="quality" optional="YES" attributeType="Integer 32" defaultValueString="0" syncable="YES"/> | ||
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/> | ||
<attribute name="uploader" optional="YES" attributeType="String" syncable="YES"/> | ||
</entity> | ||
<elements> | ||
<element name="Event" positionX="261" positionY="189" width="128" height="60"/> | ||
<element name="Video" positionX="270" positionY="207" width="128" height="105"/> | ||
</elements> | ||
</model> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// Video+CoreDataProperties.swift | ||
// | ||
// | ||
// Created by Adam Boyd on 2016-05-30. | ||
// | ||
// | ||
// Choose "Create NSManagedObject Subclass…" from the Core Data editor menu | ||
// to delete and recreate this implementation file for your updated model. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
extension Video { | ||
|
||
@NSManaged var title: String? | ||
@NSManaged var quality: NSNumber? | ||
@NSManaged var uploader: String? | ||
@NSManaged var created: NSDate? | ||
|
||
} |
Oops, something went wrong.