ParseSwift - Best Practice for Parent / Child relationship of same object

So I’ve been converting my existing code running the older Parse iOS SDK to ParseSwift, unfortunately I get this error as I have an Optional parent field for my Location object as Locations can have a parent Location.

In Parse, I have “parent” as a pointer to Location.

Fortunately it’s early enough in the development phase that I can change this.

What’s the best practice for situations like these?

Should I just store “parent” as a String with the objectID of the Location?

thanks in advance.

Can you post your code for this?

My guess, without seeing your code is an answer I posted to a similar issue posted recently. Please look through the whole thread to see your options:

1 Like

thank you!

objectId it is. :slight_smile:

The last way I mentioned will save you some lines of code (you can fetch immediately) and your client objects will mirror your schema on the server:

struct Pagina : ParseObject {
    var objectId: String?
    var createdAt: Date?
    var updatedAt: Date?
    var ACL: ParseACL?
    var titolo: [String: String]?
    var parent: Pointer<Pagina>? //This will always have to be a pointer instead of an object. The same way it's represented on the server.
}
1 Like

Gotcha. Makes sense. Thank you will try to implement this on what I’m working on. :slight_smile:

help pls. How do I assign to the “parent” variable ?

I just get a

Cannot assign value of type 'Location?' to type 'Pointer<Location>?'

error in Xcode when I directly assign the object to the Pointer.

newLocation.parent                  = parentLocationObject

when I assign like so.

thanks in advance.

EDIT: is

    newLocation.parent                  = parent as! Pointer<Location>?

the correct way to assign?

Try toPointer():


newLocation.parent = try? parentLocationObject.toPointer()

So I tried using

newLocation.parent = try? parentLocationObject.toPointer()

But I get this error of it not being a valid pointer.
I’ve printed out the Location Object I have as well below.

**Optional(ParseSwift.Pointer<Lokality.Location>(__type: "Pointer", objectId: "XSGSQOxTon", className: "Location"))**

**Fatal error: Error saving: ParseError(code: ParseSwift.ParseError.Code.incorrectType, message: "This is not a valid Pointer")**

I’ve tried using a full Location object that I queried and pulled from the database, I’ve tried creating a new one (Location(objectId: “Xxxx”)).

Neither has worked. I’m not sure now what to look into on how to get this to recognize it as a Pointer

Seems like the actual object is good. I don’t know what else to do.

Edit: I don’t know if this helps but this is what my Logs show

Jun 18, 2021, 23:07:16 +08:00- ERROR
This is not a valid PointerError: This is not a valid Pointer
    at getObjectType (/opt/app-root/src/node_modules/parse-server/lib/Controllers/SchemaController.js:1393:11)
    at getType (/opt/app-root/src/node_modules/parse-server/lib/Controllers/SchemaController.js:1317:14)
    at SchemaController.validateObject (/opt/app-root/src/node_modules/parse-server/lib/Controllers/SchemaController.js:1084:24)
    at loadSchema.then.then (/opt/app-root/src/node_modules/parse-server/lib/Controllers/DatabaseController.js:443:21)
    at process._tickCallback (internal/process/next_tick.js:68:7)

This seems to be a bug in the Swift SDK. You can keep track of the issue here:

This may take some time to fix, so I recommend using the first way I mentioned with the objectId

1 Like

@jayson thanks for reporting the error.

Can you try Swift SDK v1.8.3 and let us know if your issue is resolved?

1 Like

Will do. thanks! been racking my brain on that. I’ll update you asap.

I’ve tested saving a few items and it seems to work fine now. Thank you very much sir!

I’ll keep an eye out on this one if it ever bugs up again.

1 Like

Great! This particular bug won’t come up again as I’ve added test cases in the SDK to verify this doesn’t break.

Definitely let us know if you find any other bugs or have questions.

1 Like