1

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?

Community
  • 1
  • 1
Vicky Arora
  • 501
  • 2
  • 7
  • 20
  • You wouldn't repeatedly update the actual parse data. Just filter those stores out in your query operation – Paulw11 Nov 01 '15 at 19:50
  • @Paulw11 I don't get it. How can I do that? The thing is, If have had the storeStatus in the "Main" class I can query using whereKey "storeStatus"... But without having the status how can I query? And just to be clear, a store owner can intensionally close the store due to some reason. I just want to give him that feature. I do have the facility to hide the individual items. But than, if the store is closed, its better to hide everything from that store at one go. So if the storeStatus = false, everything will be hidden (to customer browsing products online). – Vicky Arora Nov 01 '15 at 20:08
  • 1
    You are confusing your functionality with how you store data. First, rather than storing a numeric `storeID` in that column you should store a reference to a Store object (this is your user class at the moment, but really should be a separate object class). Then you can create a query that searches for stores that are open (trivial storeStatus==true) and specify this query in `whereKey:matchesQuery:` in your product query operation – Paulw11 Nov 01 '15 at 20:20
  • So from what you said, in this type of situations, I should have a new Class for each store with there products and status stored inside that class? Later should using pointers to display data to customers? Sorry if I am being dumb, but I am still learning. Any tutorial I can use to improve my coding style in these kind of situations? Oh and thanks for explaining. I will also look for something if I can find online. – Vicky Arora Nov 01 '15 at 20:34
  • 1
    Although Parse isn't a relational database it has relations and so some normalisation makes sense; Think of the Parse data store design as how you would approach object design in your app. A store is an object. A user is an object. A product is an object. A user may be associated with 1 or more stores. A store with 1 or more users (multiple managers). A product is associated with 1 store (Or you can go further and abstract products from inventory). You may not want to do all of this for a demo. The store has a status and a reference to its user(s). A product has a reference to store – Paulw11 Nov 01 '15 at 21:21
  • That really makes sense. Thank you so much for giving me time. It was really helpful. I will do more research on this. – Vicky Arora Nov 01 '15 at 21:31

1 Answers1

1

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)
        }
    }
}
Alexander Li
  • 781
  • 4
  • 12
  • I am getting error at bestBuyStore["storeStatus"] = storeStatus. same if I use ... = PFUser.CurrentUser()?["storeStatus"] The error is "cannot assign to immutable expression of type 'AnyObject?!'". know whats could be the issue? – Vicky Arora Nov 01 '15 at 21:11
  • Try using the Parse accessor to set the storeStatus instead – Alexander Li Nov 01 '15 at 21:23
  • 1
    'bestBuyStore.setObject(storeStatus, forKey: "storeStatus")' – Alexander Li Nov 01 '15 at 21:23