Skip to content

Commit

Permalink
[Feat] 최근 검색 이력 관리를 위한 Local DB - #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jin0331 committed Jan 29, 2025
1 parent 7c5117b commit 770f7ba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
//

import SwiftUI
import RealmSwift
import ComposableArchitecture

struct SearchView: View {
@Perception.Bindable var store: StoreOf<SearchFeature>
@ObservedResults(LatestSearch.self, sortDescriptor: SortDescriptor(keyPath: "regDt", ascending: false)) var latestSearch

var body: some View {

Expand Down
41 changes: 41 additions & 0 deletions Whidy-iOS/Repository/Entity/LatestSearch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// LatestSearch.swift
// Whidy-iOS
//
// Created by JinwooLee on 1/29/25.
//

import RealmSwift
import Foundation

final class LatestSearch: Object, ObjectKeyIdentifiable {
@Persisted(primaryKey: true) var _id: ObjectId
@Persisted var keyword: String
@Persisted var regDt: Date = Date()
}

extension LatestSearch {
// static을 사용해 타입 프로퍼티로 선언
// 여기서 선언한 realm을 사용해 저장, 삭제등을 진행한다.
private static var realm = try! Realm()

// realm객체에 값을 추가
static func addRow(_ search: LatestSearch) {
try! realm.write {
realm.add(search)
}
}

// realm객체의 값을 삭제
static func delRow(_ search: LatestSearch) {
try! realm.write {
realm.delete(search)
}
}

static func delRowAll() {
try! realm.write {
realm.deleteAll()
}
}
}

0 comments on commit 770f7ba

Please sign in to comment.