从另一个视图 SwiftUI 修改视图时出错
Posted
技术标签:
【中文标题】从另一个视图 SwiftUI 修改视图时出错【英文标题】:Error when modifying a View from another View SwiftUI 【发布时间】:2020-05-08 22:32:44 【问题描述】:我搜索了很多有关此错误的信息,但似乎没有解决方案...
UITableView was told to layout its visible cells and other contents without being in the
view hierarchy (the table view or one of its superviews has not been added to a window).
This may cause bugs by forcing views inside the table view to load and perform layout without
accurate information (e.g. table view bounds, trait collection, layout margins, safe area
insets, etc), and will also cause unnecessary performance overhead due to extra layout passes.
Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in
the debugger and see what caused this to occur, so you can avoid this action altogether if
possible, or defer it until the table view has been added to a window
这是我的实际代码:
struct ContentView: View
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: FavoriteBooks.getAllFavoriteBooks()) var favoriteBooks:FetchedResults<FavoriteBooks>
@ObservedObject var bookData = BookDataLoader()
var body: some View
NavigationView
List
Section
NavigationLink(destination: FavoriteView())
Text("Go to favorites")
Section
ForEach(0 ..< bookData.booksData.count) num in
HStack
Text("\(self.bookData.booksData[num].titolo)")
Button(action:
**let favoriteBooks = FavoriteBooks(context: self.managedObjectContext)
favoriteBooks.titolo = self.bookData.booksData[num].titolo**
)
Image(systemName: "heart")
struct FavoriteView: View
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(fetchRequest: FavoriteBooks.getAllFavoriteBooks()) var favoriteBooks:FetchedResults<FavoriteBooks>
var body: some View
List
**ForEach (self.favoriteBooks) book in
Text("\(book.titolo!))")**
我刚刚选择了粗体是什么导致了这个错误,我不知道如何避免它,因为如果我启动应用程序它不会崩溃,但我不能做任何事情。 提前致谢
【问题讨论】:
【参考方案1】:这里有几个问题。第一个是,当您使用ForEach
时,如果您的内容应该能够更改(使用FetchRequest
它是......)那么FavoriteBooks
需要是Identifiable
或者您需要通过在你的id
。你实际上在代码中做了两次:
ForEach(0 ..< bookData.booksData.count) num in
// SwiftUI thinks this content never changes because it doesn't know how to resolve those changes. you didn't tell it
应该是:
ForEach(0 ..< bookData.booksData.count, id: \.self) ...
注意现在你告诉它 id 是什么。如果 bookData.booksData
的计数发生变化,现在 SwiftUI 可以解决这些变化。但实际上,为什么在这种情况下需要专门的索引呢?为什么不只是:
ForEach(bookData.booksData) book in ...
如果你使这个对象类型符合Identifiable
,你现在已经拥有了这本书。
现在讨论另一个问题,您的按钮操作。为什么要在这里重新执行 CoreData 查询?你有你想要的对象的Set
。这是只使用ForEach(bookData.booksData)
的另一个原因,您不必在此处解析索引。但总的来说,您应该永远需要重新执行核心数据查询来查找特定对象。这实际上会触发整个视图层次结构的另一个更新,这可能是您收到错误的原因。你不应该这样做。
【讨论】:
以上是关于从另一个视图 SwiftUI 修改视图时出错的主要内容,如果未能解决你的问题,请参考以下文章