2016-08-05 84 views
0

我有一個PFObjectAccount,它包含一個User的數組,它們是PFUsers的子類。 User然後有一個NSDictonary屬性,allowableApps,這是一個NSDictionary的數組,其中他們的數組包含PFObject s。PFObject上的關係查詢

所以,作爲一個結構:

帳戶

var users: [User] 

指向....

用戶

// Each key is an array of AllowApp 
var allowableApps: NSMutableDictionary 

指向...

AllowableApp

var appName: String 
var appURL: String 
var isAllowed: Bool 

我想在一個查詢來獲取所有這些關係到AllowableApp。我試過使用這樣的.includeKey

accountQuery?.includeKey("users") 
accountQuery?.includeKey("allowableApps") 

哪沒有工作。我也試過:

accountQuery?.includeKey("users.allowableApps.appName") 
accountQuery?.includeKey("users.allowableApps.appURL") 
accountQuery?.includeKey("users.allowableApps.isAllowed") 

我嘗試填充所有的AllowableApp對象UITableView,但是我得到這個錯誤:

Key "appName" has no data. Call fetchIfNeeded before getting its value. 

我的理解,我需要獲取所有的他們在嘗試訪問appName財產之前。 (我試圖設置cellForRowAtIndexPath)。


這裏是我完整的查詢:

let currentUser = User.currentUser() 
     let accountQuery = Account.query() 

     accountQuery?.whereKey("primaryUser", equalTo: currentUser!) 
     accountQuery?.includeKey("users.allowableApps") 

     accountQuery?.getFirstObjectInBackgroundWithBlock({ (account, error) in 

      if (error != nil) { 
       completion(users: nil, error: error) 
      } 
      else { 
       let users = (account as? Account)!.users 
       completion(users: users, error: nil) 
      } 
     }) 

我的想法,現在是剛剛通過的所有AllowableApp對象的循環中調用viewDidAppearfetchInBackgroundWithBlock。然後一旦他們全部加載,我重新加載表格數據。

這似乎真的很混亂和一個共同的問題。有沒有更好的解決方案,我只是沒有看到?

回答

0

從我的理解有以下結構:

  • 帳戶
    • 用戶(用戶的陣列)
      • AllowsableApps(AllowApps數組)

首先將NSMutableDictionary更改爲Array。 NSMutableDictionary是一個鍵值對,在解析時你應該創建一個字段。所以你可以使用ArrayApp的AllowApps,它會做同樣的效果。

爲了在每次需要建立以下查詢帳戶,每個用戶允許的應用程序來獲取所有帳戶和用戶:

// You can do it with sub classing if you want 
    let query = PFQuery(className: "Account") 
    query.includeKey("users.allowableApps") 
    query.findObjectsInBackgroundWithBlock { 
     (objects: [PFObject]?, error: NSError?) -> Void in 
    } 

現在爲您的用戶陣列。如果您的用戶陣列是需要登錄應用程序的用戶,最好繼承PFUser而不是從PFObject因爲PFUser包含用於處理應用程序中用戶的所有邏輯。

+0

嘿感謝您的回覆!如果有意義的話,我的結構更多的是'Account' - >'Users' - >'AllowableApps'。此外,我正在使用'NSMutableDictionary',因爲他們的鍵是我的表中的節標題視圖。我想我理論上可以有一個'AppCategory',它是一個'PFObject',它有一個'AllowableApps'數組,但它看起來很健壯,只是持有一個類別名稱。 – random

+1

嗨,是的,允許的應用程序應該在用戶下,我想我的縮進現在工作得很好:)(我會更新我的答案)。無論如何,請根據答案嘗試更改您的代碼,因爲我指的是與您相同的結構。 –

+0

我已更新我的問題以獲取正在運行的完整查詢。當我嘗試你的建議代碼時,我得到一個錯誤「只能包含指針字段」? – random