Pinning relation pointer data

A query returns containing 1 field that is a relation to another class, which I would like to pin into the Local Datastore.

If I use a SQLite browser, I can see this pinned class / query result. But I cannot see the “related” class.

My understanding from the documentation is that:

Pinning a PFObject is recursive, just like saving, so any objects that are pointed to by the one you are pinning will also be pinned.

https://docs.parseplatform.org/ios/guide/#pinning

But that doesn’t seem to be happening here. Are Relations handled differently?

Could you share the code you are using? You probably need to also query and pin the relation data.

func getCurrentComp(withId compId: String, isLocal: Bool) {
        let query = PFQuery(className: "Competition")
        query.includeKey("Rounds") // THIS IS THE RELATION FIELD - TESTING IF THIS WOULD HELP - IT DOESN'T
        
        if isLocal {
            query.fromLocalDatastore()
        }
        
        
        query.getObjectInBackground(withId: compId, block: { (object, error) in
            if let error = error {
                print("Get Current Comp Error: \(error)")
            }
            
            if let compObject = object {
                print("Get Current Comp compObject: \(compObject)")
                if !isLocal {
                    
                    compObject.pinInBackground(withName: "CurrentComp") { (success, error) in
                        if let error = error {
                            print("Get Current Comp Pin Error: \(error)")
                        }
                        
                        if success {
                            print("Get Current Comp Pinned")
                        }
                    }
                }
                self.dataManager.selectedComp = compObject
            }
        })
    }

Which returns (from print statements):

Get Current Comp compObject: <Competition: 0x600003bc6820, objectId: CXy40U65Z9, localId: (null)> {
Name = “Competition 1”;
Played = “2020-08-27 09:19:16 +0000”;
Rounds = “<PFRelation: 0x600001f92c80, 0x0.(null) -> Course>”;
}
Get Current Comp Pinned

I have been testing using Arrays of objects (classes?), rather than Parse Relations to create this relationship, taken again from the iOS documentation.

The pinning recursion seems to function as I had originally expected from the documentation in this way. It was a simple test with 2 classes:

TestClass1 - created 2 objects
TestClass2 - created 1 object, with a “Linked” field of type Array

Query for the first (single) TestClass2 object, to assign it as a PFObject.

Then testClass2.add(linkedClass, forKey: "Linked")

Now if I query for this object again AND pin it, when I view this in the SQLite browser I can see both classes having been pinned (TestClass2 has 1 row / object, TestClass1 has 2 rows / objects). Nothing else was required.

Should this pinning of related classes / objects work with Relations or Pointers as well? Or will I need to perform additional sub-queries of the linked items? Is this the reason for the following statement in the documentation?

In general, using arrays will lead to higher performance and require fewer queries

You will have to do an additional query to retrieve the relations and then pin them. You can easily create a new query for your relation with something like this:

// suppose we have a book object
let book = ...

// create a relation based on the authors key
let relation = book.relationForKey("authors")

// generate a query based on that relation
let query = relation.query()

// now execute the query

Then you can use PFObject.pinAllInBackground(listOfObjects) to pin the results.