提问人:theMap 提问时间:12/9/2022 更新时间:12/9/2022 访问量:103
从使用 Back4App 数据库的 Parse 查询关系图像
Query relation images from Parse that uses Back4App database
问:
所以我有一个 ParseObject 设置如下 - 这是 Parse 中名为 MGLocation 的主要对象:
struct MGLocation: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var originalData: Data?
var ACL: ParseACL?
var title: String?
var category: String?
init() {}
init(objectId: String?) {
self.objectId = objectId
}
}
然后,我使用以下代码进行Codable设置:
struct Place: Codable, Identifiable {
let id: Int
var b4aId = ""
let title: String?
let category: String
init(
id: Int,
title: String?,
category: String,
) {
self.id = id
self.title = title
self.category = category
}
init(with p: MGLocation) {
self.id = atomicId.wrappingIncrementThenLoad(ordering: .relaxed)
self.b4aId = p.objectId ?? ""
self.title = p.title ?? "Undefined"
self.category = p.category ?? "Uncategorized"
}
}
然后我有以下函数,它拉入 MGLocation:
func fetchPlaces() {
let query = MGLocation.query().limit(1000)
query.find { [weak self] result in
guard let self = self else {
return
}
switch result {
case .success(let items):
self.places = items.map({
Place(with: $0)
})
case .failure(let error):
}
}
}
问题:
引入关系列的最佳方式是什么?在 MGLocation 中,我有一个名为的相关列,它可以访问另一个对象。images
这将调用包含以下列的 MGImage:
id, title, column, mime
有谁知道我如何注入和拉入相关列?所有帮助将不胜感激!
答:
我建议从这里使用 Parse-Swift:https://github.com/netreconlab/Parse-Swift,与 parse-community 相反。我是 Pase-Swift 的原始开发人员之一(您可以在贡献者列表中看到这一点),我不再支持 parse-community 版本。netreconlab 版本在错误修复和功能方面遥遥领先于其他版本,您将从 netreconlab 获得更好、更快的支持,因为我过去一直是解决大多数问题的人。
引入关系列的最佳方式是什么?在 MGLocation 中,我有一个名为 images 的相关列,它可以访问另一个对象。
我总是建议查看 Playgrounds 中的类似示例。Playgrounds 有 Roles 和 Relation 示例。假设您使用的是netreconlab上的版本。您可以使用服务器上的当前关系执行此操作的唯一方法是:4.16.2
// You need to add your struct for MGImage on your client
struct MGImage: ParseObject { ... }
struct MGLocation: ParseObject {
var objectId: String?
var createdAt: Date?
var updatedAt: Date?
var originalData: Data?
var ACL: ParseACL?
var title: String?
var category: String?
var images: ParseRelation<Self>? // Add this relation
/* These aren't needed as they are already given for free.
init() {}
init(objectId: String?) {
self.objectId = objectId
}
*/
}
//: It's recommended to place custom initializers in an extension
//: to preserve the memberwise initializer.
extension MGLocation {
// The two inits in your code aren't needed because ParseObject gives them to you
}
//: Now we will see how to use the stored `ParseRelation on` property in MGLocation to create query
//: all of the relations to `scores`.
Task {
do {
//: Fetch the updated location since the previous relations were created on the server.
let location = try await MGLocation(objectId: "myLocationObjectId").fetch()
print("Updated current location with relation: \(location)")
let usableStoredRelation = try location.relation(location.images, key: "images")
let images = try await (usableStoredRelation.query() as Query<MGImage>).find()
print("Found related images from stored ParseRelation: \(images)")
} catch {
print("\(error.localizedDescription)")
}
}
如果您的列是类型而不是类型,则可以在模型中使用,并简单地在查询中使用。您可以在此处查看更多信息: https://github.com/netreconlab/Parse-Swift/blob/325196929fed80ca3120956f2545cf2ed980616b/ParseSwift.playground/Pages/8%20-%20Pointers.xcplaygroundpage/Contents.swift#L168-L173images
[MGImage]
Relation<MGImage>
var images: [MGImage]?
MGLocation
include
评论
上一个:不符合协议
评论