2015-12-14 84 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