2015-12-14 124 views
2

在我的API上,我有一個Stands和Products之間的關係。在哪個展臺上有產品,但產品可以在不同的展臺上展示。我試圖使用Realm在我的iOS應用程序上覆制這種關係,但我似乎無法使其工作。Realm,Swift,多對多關係

擁有這種關係的目標是能夠搜索銷售特定產品的展臺。

我的模型:

class Stand: Object { 
    dynamic var id : Int = 0 
    dynamic var name : String = "" 
    dynamic var latitude : Double = 0.0 
    dynamic var longitude : Double = 0.0 
    let products = List<Product>() 

    override static func primaryKey() -> String? { 
     return "id" 
    } 
} 

class Product: Object { 
    dynamic var id : Int = 0 
    dynamic var name : String = "" 
    let stands = List<Stand>() 

    override static func primaryKey() -> String? { 
     return "id" 
    } 
} 

在做了看臺我的API請求檢索到相關的產品與它以及。當我將這些附加到支架上時,它對我的​​支架模型很好,因爲產品通常只是添加到List()中。

但是,所有的產品都是單獨創建的,沒有任何支架連接到它們。

在創建產品時,是否有辦法直接將這些支架分配給產品?就像它發生的那樣?

我目前的解決辦法是這樣的..

func retrieveAndCacheStands(clearDatabase clearDatabase: Bool?) { 
    backend.retrievePath(endpoint.StandsIndex, completion: { (response) ->() in 
     let listOfProducts : List<(Product)> = List<(Product)>() 

     func addProducts(stand: Stand, products: List<(Product)>?) { 
      for product in products! { 
       print(product.name) 
       let newProduct = Product() 
       newProduct.id = product.id 
       newProduct.name = product.name 
       newProduct.stands.append(stand) 

       try! self.realm.write({() -> Void in 
        self.realm.create(Product.self, value: newProduct, update: true) 
       }) 
      } 

      listOfProducts.removeAll() 
     } 

     for (_, value) in response { 
      let stand = Stand() 
      stand.id = value["id"].intValue 
      stand.name = value["name"].string! 
      stand.latitude = value["latitude"].double! 
      stand.longitude = value["longitude"].double! 
      for (_, products) in value["products"] { 
       let product = Product() 
       product.id = products["id"].intValue 
       product.name = products["name"].string! 
       stand.products.append(product) 
       listOfProducts.append(product) 
      } 

      try! self.realm.write({() -> Void in 
       self.realm.create(Stand.self, value: stand, update: true) 
      }) 

      addProducts(stand, products: listOfProducts) 
     } 

     print(Realm.Configuration.defaultConfiguration.path!) 

     }) { (error) ->() in 
      print(error) 
    } 
} 

此存儲看臺上增加了產品給他們。它還創建了所有產品並添加了每10個ish產品(?)的Stand。

我似乎無法弄清楚如何使這項工作。其他人知道如何解決這個問題嗎?還是更好的解決方案?

回答

2

而不是做必要的手動維護逆關係雙簿記的,你應該使用域的逆關係的機制,它提供了所有使用給定的屬性指向另一個對象的對象:

class Product: Object { 
    dynamic var id: Int = 0 
    dynamic var name: String = "" 
    // Realm doesn't persist this property because it is of type `LinkingObjects` 
    // Define "stands" as the inverse relationship to Stand.products 
    let stands = LinkingObjects(fromType: Stand.self, property: "products") 

    override static func primaryKey() -> String? { 
     return "id" 
    } 
} 

有關更多信息,請參閱Inverse Relationships上的Realm文檔。

+0

昨天我試過類似的方法。我不斷收到'額外論證'產品'通話'錯誤。我已經嘗試過你的解決方案,它完美的工作!謝謝! – Alserda

+0

@Alserda解決方案是什麼? – Piraba

+1

@Piraba我已經更新了我對Realm反向關係語法變化的回答。 – jpsim