Pulling data out of Pointer<Object> without having to fetch() again

Related to my earlier question here, I now use Pointer to store a parent Location to my Location object.

However, now that I need the name of the parent, I’m trying to figure out if there’s a way to have the object’s custom data to be included in an includeAll / include() call.

When I do

let foundLocations = try await query.limit(20).includeAll().find()

Gives me locations + their parent locations.

However, because the Location parent object is defined as such:

struct Location: ParseObject {
// Parent
**var** parent: Pointer<Location>?
}

The parent object data that I get back is only a Pointer object as expected doing a 1 level down include():

"parent":{"__type":"Pointer","className":"Location","objectId":"UBr4yqeQ4o"}

I’m wondering if there’s a way I can include the Parent Location data to be called along side my original includeAll call so I don’t have to do additional queries per object just to get the name (field) of the parent object as I can imagine this would be costly.

Is there anyway I can create a custom extension of the Pointer object so I can save the name field as well ?

Otherwise, my current line of thinking is just add another parentName field on the Location object which seems to be the easiest.

I appreciate the help,
Jayson

The reason you can’t do this is because the compiler prevents cycles and you can easily create one with your current setup. So if you want a Location property inside of Location object using the Swift SDK, you will have to fetch the property every time like you are doing. If you were pointing to a different ParseObject, you wouldn’t have this problem:

struct City: ParseObject {
  // A Parent shouldn’t be of the same ParseObject or else it has to be a Pointer
  var location: Location?
}

When saving to your server it will automatically be converted to a Pointer. When you fetch using include everything will be fetched.

You can also try:

struct Location: ParseObject {
  // Parent
  var parent: [Location]?
}

and see if it compiles, saves, and fetches with include, but I haven’t tried it myself.

Yes, unfortunately I do need it to be the same object, thus the Pointer<> method.

I’ll try out the Array method that you suggested and see which works better.

thanks again

IMO, setting up your tables in this manner isn’t good practice and the compiler throwing an error when the property is the same type as the struct is eluding to this. You are free to design your apps the way you want, but I suggest looking into ways to make your app code more efficient along with your database design (avoid designing with cyclic dependencies). Below is some more information about Swift structs:

1 Like

Thanks for the link. That helps me understand more about the logic behind the error. I’ve been looking into ways to improve my structure and will keep this in mind.

thanks again!

btw, the array method didn’t work for me.