-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlaylistController.swift
45 lines (38 loc) · 1.15 KB
/
PlaylistController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// PlaylistController.swift
// PlaylistCoreData
//
// Created by Cameron Milliken on 12/12/18.
// Copyright © 2018 Karl Pfister. All rights reserved.
//
import Foundation
import CoreData
class PlaylistController {
// MARK: Singlton
static let sharedInstance = PlaylistController() //Alawyas name this a sharedInstance
// MARK: CRUD
var playlist: [Playlist] {
let fetchRequest: NSFetchRequest <Playlist> = Playlist.fetchRequest()
return (try? CoreDataStack.context.fetch(fetchRequest)) ?? []
}
// Create
func createPlaylist(playlistWithName name: String) {
Playlist(name: name)
// Save To Persistence
saveToPersistentStore()
}
// Delete
func deletePlaylist(playlistToDelete: Playlist) {
CoreDataStack.context.delete(playlistToDelete) //Becky performs this. PSC
saveToPersistentStore()
}
// Save
func saveToPersistentStore() {
//TODO: Finish
do {
try CoreDataStack.context.save()
} catch {
print("There was an error in \(#function) : \(error.localizedDescription)")
}
}
}