简体   繁体   中英

Query relation images from Parse that uses Back4App database

So I have an ParseObject setup as this - This is the main Object in Parse called 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
    }
}

Then I have my Codable setup using the following code:

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"
    }
}

Then I have the following function which pulls in the 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):
        }
    }
}

Questions :

What is the best way that I can pull in a relational column? Inside MGLocation, I have a related column called images which can access another object.

在此处输入图像描述

This calls MGImage which has the following columns:

id, title, column, mime

Does anyone know how I can infuse and pull in the related column also? All help will be appreciated!

I recommend using Parse-Swift from here: https://github.com.netreconlab/Parse-Swift , as oppose to the parse-community one. I'm one of the original developers of Pase-Swift (you can see this on the contributors list ) and I don't support the parse-community version anymore . The.netreconlab version is way ahead of the other in terms of bug fixes and features and you will get better and faster support from the.netreconlab since I've been the one to address the majority of issues in the past.

What is the best way that I can pull in a relational column? Inside MGLocation, I have a related column called images which can access another object.

I always recommend looking at the Playgrounds for similar examples. The Playgrounds has Roles and Relation examples. Assuming you are using version 4.16.2 on.netreconlab. The only way you can do this with your current relation on your server is like so:


// 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)")
  }
}

If your images column was of type [MGImage] instead of Relation<MGImage> then you would be able to use var images: [MGImage]? in your MGLocation model and simply use include on your query. You can see more here: https://github.com.netreconlab/Parse-Swift/blob/325196929fed80ca3120956f2545cf2ed980616b/ParseSwift.playground/Pages/8%20-%20Pointers.xcplaygroundpage/Contents.swift#L168-L173

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM