Vicky Arora
Vicky Arora

Reputation: 501

Updating multiple values in selected rows of Parse.com

Working with: iOS Swift 2.0, xcode 7.1

I am working on a small demo project of retail store app. In this, each seller has more than 1 product. Sellers StoreStatus Open/Close (true/false) Bool value is stored in the "user" class. Customers can see all the sellers product from the "Main" class. Though nothing there, but I still have the picture attached of the Main and the User class in Parse.com. picture attached.

Now, lets say I want to hide all the products sold by a "BestBuy Store (Store ID 101)" when it is closed. As the "Main" class consists of "n" number of sellers and there products, I am no sure how to iterate over all the product in the "Main" class, filter BestBuy Store product and set the StoreStatus bool value to false.

I read online and saw that we can use saveAllInBackground with Block in parse. But I didn't really get how to actually use that code as most of the answers are too complex for me.

for ex: Link1, Link2

Parse.com has the following in Objective C:

+ (void)saveAllInBackground:(PF_NULLABLE NSArray PF_GENERIC ( PFObject *) *)objects block:(PF_NULLABLE PFBooleanResultBlock)block

Can some one help me in this?

Upvotes: 1

Views: 642

Answers (1)

Alexander Li
Alexander Li

Reputation: 791

I hope this can help. This is how you would get all of the stores with a specific Id and change all of their store statuses.

func updateStoreStatus(storeId:Int, to storeStatus:Bool) {
    let query = PFQuery(className: "Main")
    query.whereKey("StoreID", equalTo: storeId)

    //Find all stores with a specific storeId
    query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if error == nil {
            if let bestBuyStores = objects {

                //Change all of their store statuses
                for bestBuyStore in bestBuyStores {
                    bestBuyStore.setObject(storeStatus, forKey: "storeStatus")

                    //Or if you want to set it the current user's store status
                    //bestBuyStore.setObject(PFUser.currentUser()?["storeStatus"], forKey:"storeStatus")
                }
                //Save all of them in one call to Parse
                PFObject.saveAllInBackground(bestBuyStores)
            } else {
                print("No Stores with the StoreID: \(storeId) found")
            }
        } else {
            print(error?.localizedDescription)
        }
    }
}

Upvotes: 1

Related Questions