mirror of
https://github.com/Mueller-Patrick/DHBW-Service-App.git
synced 2024-11-25 02:23:56 +00:00
Annotating and reordering Core Data Persistence Class
This commit is contained in:
parent
13d11c2c2e
commit
a26b62aa4c
|
@ -8,28 +8,20 @@
|
||||||
import CoreData
|
import CoreData
|
||||||
|
|
||||||
struct PersistenceController {
|
struct PersistenceController {
|
||||||
|
// Singleton
|
||||||
static let shared = PersistenceController()
|
static let shared = PersistenceController()
|
||||||
|
|
||||||
static var preview: PersistenceController = {
|
// Cloud Kit container
|
||||||
let result = PersistenceController(inMemory: true)
|
|
||||||
let viewContext = result.container.viewContext
|
|
||||||
for _ in 0..<10 {
|
|
||||||
let newItem = Item(context: viewContext)
|
|
||||||
newItem.timestamp = Date()
|
|
||||||
}
|
|
||||||
do {
|
|
||||||
try viewContext.save()
|
|
||||||
} catch {
|
|
||||||
// Replace this implementation with code to handle the error appropriately.
|
|
||||||
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
|
||||||
let nsError = error as NSError
|
|
||||||
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}()
|
|
||||||
|
|
||||||
let container: NSPersistentCloudKitContainer
|
let container: NSPersistentCloudKitContainer
|
||||||
|
|
||||||
|
// Managed object context
|
||||||
|
public var context: NSManagedObjectContext {
|
||||||
|
get {
|
||||||
|
return self.container.viewContext
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Constructor
|
||||||
init(inMemory: Bool = false) {
|
init(inMemory: Bool = false) {
|
||||||
container = NSPersistentCloudKitContainer(name: "DHBW_Service")
|
container = NSPersistentCloudKitContainer(name: "DHBW_Service")
|
||||||
if inMemory {
|
if inMemory {
|
||||||
|
@ -52,4 +44,23 @@ struct PersistenceController {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Preview content
|
||||||
|
static var preview: PersistenceController = {
|
||||||
|
let result = PersistenceController(inMemory: true)
|
||||||
|
let viewContext = result.container.viewContext
|
||||||
|
for _ in 0..<10 {
|
||||||
|
let newItem = Item(context: viewContext)
|
||||||
|
newItem.timestamp = Date()
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
try viewContext.save()
|
||||||
|
} catch {
|
||||||
|
// Replace this implementation with code to handle the error appropriately.
|
||||||
|
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
|
||||||
|
let nsError = error as NSError
|
||||||
|
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user