0

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.

2 Answers 2

1

Hi think you should include the "sentence" to the Query response by using:

query.includeKey("sentence") query.findObjectsInBackground { ...

it will help the query to include sentence as PFObject to the result. This has an effect similar to a join. You can use dot notation to specify which fields in the included object are also fetch.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.