2015-11-01 42 views
1

中所選行與工作:雨燕的iOS 2.0時,Xcode 7.1更新多個值Parse.com

我的工作在零售商店的應用程序的一個小示範項目。在這裏,每個賣家有超過1個產品。賣家StoreStatus打開/關閉(true/false)布爾值存儲在「用戶」類中。客戶可以看到「主」類中的所有賣家產品。儘管沒有任何內容,但我仍然在Parse.com中添加了Main和User類的圖片。 picture attached

現在,可以說我想在關閉時隱藏「BestBuy Store(Store ID 101)」銷售的所有產品。由於「Main」類由「n」個賣家及其產品組成,因此我不確定如何遍歷「Main」類中的所有產品,過濾BestBuy Store產品並將StoreStatus布爾值設置爲false。

我在線閱讀,看到我們可以在解析中使用saveAllInBackground和Block。但是我並沒有真正懂得如何實際使用該代碼,因爲大部分答案對我來說都太複雜了。

爲前:Link1Link2

Parse.com具有以下目標C:

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

可有一個人幫我在這?

+0

您不會重複更新實際的分析數據。只需在您的查詢操作中過濾這些商店 – Paulw11

+0

@ Paulw11我不明白。我怎樣才能做到這一點?事情是,如果有「Main」類中的storeStatus,我可以使用whereKey「storeStatus」查詢......但是沒有狀態,我該如何查詢?而且要明確一點,店主可能由於某種原因故意關閉店鋪。我只想給他那個功能。我確實有隱藏單個項目的功能。但是,如果商店關門了,那麼最好是一次性從商店隱藏所有東西。因此,如果storeStatus = false,則所有內容都將隱藏(向在線客戶瀏覽產品)。 –

+1

您正在將自己的功能與存儲數據的方式混淆在一起。首先,不應該在該列中存儲數字「storeID」,而應該存儲對Store對象的引用(目前這是您的用戶類,但實際上應該是單獨的對象類)。然後,您可以創建一個查詢,查找打開的商店(普通的storeStatus == true),並在產品查詢操作中的whereKey:matchesQuery中指定此查詢。 – Paulw11

回答

1

我希望這可以幫助。這就是如何讓所有商店獲得特定的ID並更改所有商店的狀態。

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) 
     } 
    } 
} 
+0

我得到錯誤bestBuyStore [「storeStatus」] = storeStatus。同樣如果我使用... = PFUser.CurrentUser()?[「storeStatus」]錯誤是「無法分配給類型'AnyObject ?!'的不可變表達式」。知道什麼可能是問題? –

+0

嘗試使用Parse訪問器來設置storeStatus代替 –

+1

'bestBuyStore.setObject(storeStatus,forKey:「storeStatus」)' –