iosswiftparse-serverpfquery

Testing pointer equality with PFQuery, wrong syntax


Here is some Parse-Server related swift code that does not work, probably only because of some obvious syntax mistake.

If someone could point out the issue I would be very happy and grateful.

func getSentenceTranslations(_ sentences:[PFObject]) {
    let query = PFQuery(className: "TranslationsList")
    query.whereKey("sentence", containedIn: sentences)
    query.addAscendingOrder("order")
    query.findObjectsInBackground {
        [weak self] (objects: [PFObject]?, error: Error?) in
        if error != nil {
            print("Error in \(#function)\n" + (error?.localizedDescription)!)
            return
        }

        // The line below prints the expected number (> 0).
        print("\(objects?.count ?? 99) translations found.")

        for sentence in sentences {
            for translation in objects! {
                // The following does not work!!
                if (translation.value(forKey: "sentence") as! PFObject) == sentence {
                    print("FTF: \(sentence.value(forKey: "sentence")!)") // MISSING!!
                }
            }
        }
    }
}

But the fact is that if I stop the debugger at the right moment, I can see that there should be a hit on the line commented (MISSING!!). Here is what the debugger shows:

(lldb) p sentence.debugDescription
(String) $R31 = "<SentencesList: 0x1073j7450, objectId: krxX9WsZuxK, localId: (null)> {\n    order = 3;\n    owner = Ht8AbcR543;\n    sentence = \"Hello big world of things.\";\n}"
(lldb) p translation.debugDescription
(String) $R32 = "<TranslationsList: 0x10739f0e0, objectId: FoBdjoPF1n, localId: (null)> {\n    order = 0;\n    owner = Ht8AbcR543;\n    sentence = \"<SentencesList: 0x1073aa8c0, objectId: krxX9WsZuxK, localId: (null)>\";\n    translation = \"Die Welt von immer!\";\n}"
(lldb) 

We can see that the value krxX9WsZuxK is found both on sentence (objectId) and on translation (sentence field) so I expect a line like this:

FTF: .......

to be printed and this does not happen. So I suspect there is a mistake in the line:

if (translation.value(forKey: "sentence") as! PFObject) == sentence {

I tried various other variations all failing.


Solution

  • After searching and trying many things, here is what worked:

    I needed to replace this line:

      if (translation.value(forKey: "sentence") as! PFObject) == sentence {
    

    by this:

      if (translation.value(forKey: "sentence") as! PFObject).value(forKey: "objectId")  as! String ==
          sentence.value(forKey: "objectId") as! String {
    

    It makes perfect sense, but I was hoping for some kind of short hand writing. Anyway, I hope it will at some point be useful to someone else too.