comparison Simoleon/Persistence.swift @ 189:9f044d33d8ac 2.0.0

add merge policy
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Thu, 23 Dec 2021 16:43:00 +0100
parents ba3ebe8cefe5
children
comparison
equal deleted inserted replaced
188:e4f5dcf4d596 189:9f044d33d8ac
15 let viewContext = result.container.viewContext 15 let viewContext = result.container.viewContext
16 for _ in 0..<10 { 16 for _ in 0..<10 {
17 let newItem = Item(context: viewContext) 17 let newItem = Item(context: viewContext)
18 newItem.timestamp = Date() 18 newItem.timestamp = Date()
19 } 19 }
20
21 for _ in 0..<10 {
22 let favoritePair = FavoritePair(context: viewContext)
23 favoritePair.baseCurrency = "EUR"
24 favoritePair.quoteCurrency = "USD"
25 }
20 do { 26 do {
21 try viewContext.save() 27 try viewContext.save()
22 } catch { 28 } catch {
23 // Replace this implementation with code to handle the error appropriately. 29 // Replace this implementation with code to handle the error appropriately.
24 // 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. 30 // 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.
30 36
31 let container: NSPersistentCloudKitContainer 37 let container: NSPersistentCloudKitContainer
32 38
33 init(inMemory: Bool = false) { 39 init(inMemory: Bool = false) {
34 container = NSPersistentCloudKitContainer(name: "Simoleon") 40 container = NSPersistentCloudKitContainer(name: "Simoleon")
41 container.viewContext.automaticallyMergesChangesFromParent = true
42 container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
35 if inMemory { 43 if inMemory {
36 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") 44 container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
37 } 45 }
38 container.loadPersistentStores(completionHandler: { (storeDescription, error) in 46 container.loadPersistentStores(completionHandler: { (storeDescription, error) in
39 if let error = error as NSError? { 47 if let error = error as NSError? {