티스토리 뷰

다음과 같이 FetchedResults를 프로퍼티로 가지는 뷰를 만들었다. 하지만 도저히 미리보기에서 FetchedResults를 만드는 방법이 떠오르지 않았다.

struct ItemsView: View {
    var items: FetchedResults<Item>
    ....
}

struct ItemsView_preview: PreviewProvider {
    static var previews: some View {
        ItemsView(items: ?????)
            .environment(\.managedObjectContext, viewContext)
    }
}

 

내가 가진 모든 지식을 사용해서 FetchedResults를 만들어 보았지만 실패하고 말았다.

구글링을 통해서 얻은 방법은 두 가지가 있었다.

https://stackoverflow.com/questions/61495141/passing-core-data-fetchedresultst-for-previews-in-swiftui

 

Passing Core Data FetchedResults<T> for previews in SwiftUI

I have a parent view which does a @FetchRequest and passes the FetchedResults<T> to a child view. Everything works, and the child view is able to parse through the FetchedResults. However, I ...

stackoverflow.com

https://peterfriese.dev/posts/swiftui-previews-interactive/

 

Previewing Stateful SwiftUI Views - Interactive Previews for your SwiftUI views | Peter Friese

Enjoyed reading this article? Subscribe to my newsletter to receive regular updates, curated links about Swift, SwiftUI, Combine, Firebase, and - of course - some fun stuff 🎈

peterfriese.dev

 

그리고 다음과 같이 해결하였다.

struct ItemsView: View {
    var items: FetchedResults<Item>
    ....
}

struct ItemsView_preview: PreviewProvider {
    struct Content: View {
        @FetchRequest( .... )
        private var items: FetchedResult<Item>
        
        var body: some View {
            ItemsView(items: items)
        }
    }
    
    static var previews: some View {
        Content()
            .environment(\.managedObjectContext, viewContext)
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/06   »
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
글 보관함