-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conversion.swift
97 lines (82 loc) · 3.18 KB
/
Conversion.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Conversion+CoreDataClass.swift
// Finch
//
// Created by Nicholas Bellucci on 12/15/20.
//
//
import Foundation
import CoreData
import Combine
@objc(Conversion)
public class Conversion: NSManagedObject {
@nonobjc public class func createBlank(context: NSManagedObjectContext) -> AnyPublisher<NSManagedObjectContext, Error> {
Future<NSManagedObjectContext, Error> { promise in
let entity = NSEntityDescription.entity(forEntityName: "Conversion", in: context)
let newConversion = NSManagedObject(entity: entity!, insertInto: context)
newConversion.setValue(UUID(), forKey: "id")
newConversion.setValue("", forKey: "name")
newConversion.setValue("", forKey: "json")
newConversion.setValue("", forKey: "model")
newConversion.setValue(0, forKey: "language")
do {
try context.save()
promise(.success(context))
} catch {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
@nonobjc public class func fetchAll(context: NSManagedObjectContext) -> AnyPublisher<[Conversion], Error> {
Future<[Conversion], Error> { promise in
let request = Conversion.createFetchRequest()
request.returnsObjectsAsFaults = false
do {
let conversions = try context.fetch(request)
promise(.success(conversions))
} catch {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
@nonobjc public class func saveData(conversionId: String, context: NSManagedObjectContext, name: String, json: String, model: String, language: Language) -> AnyPublisher<NSManagedObjectContext, Error> {
Future<NSManagedObjectContext, Error> { promise in
let request = Conversion.createFetchRequest()
request.predicate = NSPredicate(format: "id = %@", conversionId)
do {
let conversions = try context.fetch(request)
if let conversion = conversions.first {
conversion.setValue(name, forKey: "name")
conversion.setValue(json, forKey: "json")
conversion.setValue(model, forKey: "model")
conversion.setValue(language.rawValue, forKey: "language")
do {
try context.save()
promise(.success(context))
} catch {
promise(.failure(error))
}
} else {
// promise(.failure())
}
} catch {
promise(.failure(error))
}
}
.eraseToAnyPublisher()
}
}
extension Conversion {
@nonobjc public class func createFetchRequest() -> NSFetchRequest<Conversion> {
return NSFetchRequest<Conversion>(entityName: "Conversion")
}
@NSManaged public var id: UUID
@NSManaged public var name: String
@NSManaged public var json: String
@NSManaged public var model: String
@NSManaged public var language: Int16
}
extension Conversion: Identifiable {
}