1

I have a table called table 1 which contains a field called parent field which contains object(Objectid) of table 2.Now i dont want to get the duplicated objectId's and arrange them on the basis of ascending order.Here is the data of the table.

 Table1
 parentfield(id)
 790112
 790000
 790001
 790112
 790000
 790001

Now the result would be the first three elements but i dont know the number of id's matched.Is there a way to do that?

2 Answers 2

5

Unfortunately, there is not SELECT DISTINCT / GROUP BY operation in Parse.

See this thread: https://parse.com/questions/retrieving-unique-values

Suggested team solution:

There's no built-in query constraint that would return distinct values based on a column.

As a workaround, you can query for all the rows, then iterate through them and track the distinct values for the desired column

So, the sad, bad, horrible idea, is to make a Cloud Function that fetch all the possible elements ( keep in mind that Parse allow you to fetch at maximum 1000 elements for each query ) and then remove the duplicates from the resulting list. You can do this all on the Cloud code function, and then returning the cleaned list to the client, or you can do this directly on your client devices. All this means that if you want to retrieve the real select distinct equivalent at this conditions, you should first fetch all the element ( a loop of query, retrieving 1000 items at time ) and then apply your custom algorithm for removing the duplicates. I know, it's really long and frustrating, considering the fact that the Parse cloud function execution has a timeout limit of 7-10 seconds. Maybe moving to the Parse backgroud jobs, you can populate a distinct temporary table, since you should have up to 15 minutes of execution before the timeout.

Another drastic solution is to move your data on another server that support an ER databases ( like on Openshift, that keep a free tier ) and with some Parse background job, you synchronize the elements from parse to the ER db, so you redirect the client request to the ER db instead of Parse.

Hope it helps

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

2 Comments

ohhh okay i thought there was a better solution then iterating but thanks for the descriptive answer now i don't have to look else where for this answer..Anyways thanks for the awesome and descriptive explanation.
It's really sad that they didn't included the distinct feature. Hope they'll do that in the near future.
0

i have an app with similar requirements to display each company name only once and the number of items it has

here is how I implemented the solution at client side (swift 3), my database is relatively small:

     var companyItemsCount = [Int: Int]() // stores unique companyId and number of items (count)

     query.findObjectsInBackground(block: { (objects: [PFObject]?, error: Error?) in

        var companyId: Int = 0

        if error == nil {
            // The find succeeded.
            // Do something with the found objects
            if let objects = objects {
                for object in objects {

                    companyId = object["companyId"] as! Int

                    if self.companyItemsCount[companyId] == nil {

                        self.companyNames.append(object["companyName"] as! String)
                        self.companyIds.append(object["companyId"] as! Int)

                        self.companyItemsCount[companyId] = 1

                    }else{

                        self.companyItemsCount[companyId]! += 1
                    }

                    self.tableView.reloadData()

                }

            }

        } else {

            // Log details of the failure
            print("Error: \(error!) \(error!.localizedDescription)")
        }
    })

    self.tableView.reloadData()

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.