Fetch on an array of pointers #137
Replies: 4 comments 4 replies
-
This all depends on your particular Parse Class setup on your server, but the Swift Playgrounds is the first place to check. If you designed your schema to be [ParseObject] you can use the example in playgrounds |
Beta Was this translation helpful? Give feedback.
-
Perhaps I'm misunderstanding, but the playground didn't really help. Say I have a Parse Class called Movie, and one called Actor, as so: public struct Movie: ParseObject {
// ...
// default variables
// ...
public var movieName: String?
public var leadActor: Pointer<Actor>?
}
public struct Actor: ParseObject {
// ...
// default variables
// ...
public var actorName: String?
public var mostFamousMovie: Pointer<Movie>?
} If I ran a query on the Movie table, for example, where guard let movies = try? await query.find()
else { return }
let pointers = movies.compactMap(\.leadActor)
let actors: Array<Actor> = pointers.map { $0.toObject() }
// I know I could have also done:
// let actors: Array<Actor> = movies.compactMap { $0.leadActor?.toObject() }
guard let fetchedActors = try? await actors.fetchAll()
else { return } Is this the best/most efficient way to do this? When I first wrote the question, I was thinking I had to loop over the array of Movie, and then call fetch() on each leadActor pointer. guard let movies = try? await query.fetch()
else { throw /*some Error*/ }
var actors: Array<Actor> = []
for movie in movies {
guard let actor = try? await movie.leadActor?.fetch()
else { continue }
actors.append(actor)
} Because I noticed there was no |
Beta Was this translation helpful? Give feedback.
-
The playgrounds examples shows you don’t need public struct Movie: ParseObject { And then query with fetching all to get everything at once. |
Beta Was this translation helpful? Give feedback.
-
Okay, thanks for your thoughts |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there an easy way to convert an
Array<Pointer<ParseObject>>
into anArray<ParseObject>
without looping over it and calling fetch() of each pointer?Beta Was this translation helpful? Give feedback.
All reactions