问题描述
所以我有这个解析查询:let query = Icons.query()
现在我想加载加载到 SwiftUI 列表中的每个对象的数据。
我知道查询是有限的,那么我怎样才能加载所有图标?目前大约有 310 个对象,但稍后会扩展。
请注意,我使用的是新发布的 PURE ParseSwift SDK,所以我认为以后会更多地问这个问题。
我现在就是这样做的,但是如果我的“图标”有 1000 多个项目,我就会遇到一些图标无法显示的问题。
struct AIQuick: View {
var category: String
@State private var icons: [Icons] = []
var body: some View {
List {
ForEach(icons,id: \.self) { icon in
NavigationLink(destination: AIItem(icon: icon,category: category)) {
AIQuickRow(icon: icon)
}
}
}
.onAppear(perform: {
let query = Icons.query().order([.ascending("name")])
query.limit(1000).find() { results in
switch results {
case .success(let icons):
self.icons = icons
case .failure(let error):
assertionFailure("Error querying: \(error)")
}
}
})
}
}
感谢您的帮助!
解决方法
根据您提到的用例,您可以使用 LazyVStack 或 List。我认为没有足够的信息来推荐一个。
如何正确地从 ParseSwift 查询以将数据加载到列表中?
您是否看到用于填充视图的 playgrounds example on Parse-Swift using live query?这可以直接用于广告“视图模型”:
//: Create a query just as you normally would.
var query = GameScore.query("score" > 9)
//: To use subscriptions inside of SwiftUI
struct ContentView: View {
//: A LiveQuery subscription can be used as a view model in SwiftUI
@ObservedObject var subscription = query.subscribe!
var body: some View {
VStack {
if subscription.subscribed != nil {
Text("Subscribed to query!")
} else if subscription.unsubscribed != nil {
Text("Unsubscribed from query!")
} else if let event = subscription.event {
//: This is how you register to receive notificaitons of events related to your LiveQuery.
switch event.event {
case .entered(let object):
Text("Entered with score: \(object.score)")
case .left(let object):
Text("Left with score: \(object.score)")
case .created(let object):
Text("Created with score: \(object.score)")
case .updated(let object):
Text("Updated with score: \(object.score)")
case .deleted(let object):
Text("Deleted with score: \(object.score)")
}
} else {
Text("Not subscribed to a query")
}
Spacer()
Text("Update GameScore in Parse Dashboard to see changes here")
Button(action: {
try? query.unsubscribe()
},label: {
Text("Unsubscribe")
.font(.headline)
.background(Color.red)
.foregroundColor(.white)
.padding()
.cornerRadius(20.0)
.frame(width: 300,height: 50)
})
}
}
}
您还可以创建自己的视图模型并使用标准查询(examples 或 docs 中以“Publisher”结尾的任何内容来观察查询何时完成。
我想一个典型的用例是首先使用标准查询来填充您的视图,然后订阅相同的视图并像第一个示例一样观察更改。