swift 从使用Back4App数据库的Parse查询关系图像

2skhul33  于 2022-12-10  发布在  Swift
关注(0)|答案(1)|浏览(116)

因此,我有一个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,标题,列,mime
有没有人知道我可以如何注入和拉在相关的专栏也?所有的帮助将不胜感激!

8fsztsew

8fsztsew1#

我建议从这里使用Parse-Swift:https://github.com/netreconlab/Parse-Swift,而不是parse-community。我是Pase-Swift(你可以在contributors list上看到)和I don't support the parse-community version anymore的原始开发者之一。netreconlab版本在bug修复和功能方面遥遥领先于其他版本,你将从netreconlab获得更好更快的支持,因为我在过去解决了大多数问题。
在MGLocation中,我有一个名为images的相关列,它可以访问另一个对象。
我总是建议您查看Playgrounds以获得类似的示例。Playgrounds有Roles and 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)")
  }
}

如果您的images数据行是[MGImage]类型而非Relation<MGImage>,则您可以在MGLocation模型中使用var images: [MGImage]?,而只需要在查询中使用include即可。您可以在这里看到更多信息:https://github.com/netreconlab/Parse-Swift/blob/325196929fed80ca3120956f2545cf2ed980616b/ParseSwift.playground/Pages/8%20-%20Pointers.xcplaygroundpage/Contents.swift#L168-L173

相关问题