/ / Realm, Swift, Relazione molti-a-molti - ios, rapido, regno

Reame, Swift, relazione molti-a-molti - ios, rapido, regno

Sulla mia API ho una relazione tra gli stande prodotti. In quali stand sono presenti prodotti, ma i prodotti possono essere trovati anche in stand diversi. Sto cercando di replicare questa relazione sulla mia applicazione iOS, usando Realm ma non riesco a farlo funzionare.

L'obiettivo di avere questa relazione è essere in grado di cercare Stand che vendono prodotti particolari.

Il mio modello:

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"
}
}

Quando faccio la mia richiesta API per gli stand che recuperoi prodotti associati con esso pure. Quando li aggiungo agli stand funziona perfettamente per il mio modello di stand, in quanto i prodotti vengono normalmente aggiunti in List ().

Ma tutti i prodotti sono creati individualmente, senza avere alcun supporto ad essi collegato.

C'è un modo per assegnare direttamente questi Stand ai Prodotti al momento della creazione dei Prodotti? Proprio come sta succedendo il contrario?

La mia attuale soluzione è questa ..

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

Questo memorizza gli stand e aggiunge prodotti a loro. Crea anche tutti i prodotti e aggiunge 1 stand per 10 prodotti is (?).

Non riesco a capire come farlo funzionare, qualcun altro sa come risolvere questo problema o una soluzione migliore?

risposte:

4 per risposta № 1

Invece di fare la doppia contabilità necessariaper mantenere manualmente le relazioni inverse, è necessario utilizzare il meccanismo delle relazioni inverse di Realm, che fornisce tutti gli oggetti che puntano a un altro oggetto utilizzando una determinata proprietà:

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"
}
}

Vedi i documenti di Realm su Relazioni inverse per maggiori informazioni.