-
Notifications
You must be signed in to change notification settings - Fork 1
/
APIRestDemoDataBase.swift
43 lines (37 loc) · 1.14 KB
/
APIRestDemoDataBase.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
//
// APIRestDemoDataBase.swift
// APIRestDemo
//
// Created by Adrian Iraizos Mendoza on 25/1/24.
//
import Foundation
import CoreData
final class APIRestDemoDataBase {
static let shared = APIRestDemoDataBase()
let context: NSManagedObjectContext
init(context: NSManagedObjectContext = APIRestDemoContainer.shared.container.viewContext) {
self.context = context
}
}
//MARK: DiceBear
extension APIRestDemoDataBase {
func fetchEmojis() throws -> [DiceBearEmojiModel] {
let request = EmojiEntity.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(keyPath: \EmojiEntity.createdAt, ascending: false)]
do {
let emojis = try context.fetch(request)
return emojis.compactMap { $0.emojiModel }
} catch {
throw DataBaseError.fetch
}
}
func insert(emoji: DiceBearEmojiModel) throws {
let newEmojiEntityItem = emoji.getEmojiEntityItem(from: emoji, in: context)
context.insert(newEmojiEntityItem)
do {
try context.save()
} catch {
throw DataBaseError.insert
}
}
}