Skip to content

Commit

Permalink
feat: fetchDiary 메서드 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
hemg2 committed Sep 6, 2023
1 parent e9a475c commit ac114b1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions Diary/Controller/CreateDiaryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ extension CreateDiaryViewController {
}
}

// MARK: - UITextViewDelegate
extension CreateDiaryViewController: UITextViewDelegate {
func textViewDidEndEditing(_ textView: UITextView) {
let contents = textView.text.split(separator: "\n")
Expand Down
2 changes: 1 addition & 1 deletion Diary/Controller/DiaryListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ final class DiaryListViewController: UIViewController {
extension DiaryListViewController: AlertDisplayable {
private func readCoreData() {
do {
let fetchedDiaries = try container.viewContext.fetch(Diary.fetchRequest())
let fetchedDiaries = try CoreDataManager.shared.fetchDiary()
diaryList = fetchedDiaries.filter { $0.title != nil }
tableView.reloadData()
} catch {
Expand Down
9 changes: 9 additions & 0 deletions Diary/DataManager/CoreDataManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import CoreData
class CoreDataManager {
static let shared = CoreDataManager()
private init() {}

func fetchDiary() throws -> [Diary] {
let request: NSFetchRequest<Diary> = Diary.fetchRequest()
let sortByDate = NSSortDescriptor(key: "createdAt", ascending: false)
request.sortDescriptors = [sortByDate]

let diaries = try persistentContainer.viewContext.fetch(request)
return diaries
}

func createDiary() -> Diary {
let newDiary = Diary(context: persistentContainer.viewContext)
Expand Down

0 comments on commit ac114b1

Please sign in to comment.