0

My goal is to develop this relationship with realm swift: relationship between two data sets in realm for swift

I have a dataset with all characters. Then I have a few lists, to which I want to add certain characters to.

This is what I got so far in swift code:

//
//  Lists.swift
//
//  Created by Guest on 28.01.19.
//  Copyright © 2019 Dom. All rights reserved.
//
// https://academy.realm.io/posts/realm-primary-keys-tutorial/

import Foundation
import RealmSwift

class Lists: Object {
    // making use of primary keys
    @objc dynamic var listID = UUID().uuidString
    @objc dynamic var listName : String = ""
    @objc dynamic var listDesc : String = ""
    @objc dynamic var listProgressPercentage : Int = 0
    // lists all characters associated with this list
    var charIDs = List<Characters>()

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

And:

//
//  Characters.swift
//
//  Created by Guest on 28.01.19.
//  Copyright © 2019 Dom. All rights reserved.
//

import Foundation
import RealmSwift

class Characters: Object {
    // get charID from dictionary.txt later
    @objc dynamic var charID        = 0
    @objc dynamic var character     : String = ""

}

My question: - Lists is still empty. How do it now add a new list and add e.g. 25 different object references to the objects in "Characters"?

Appreciate your help!

ItsMeDom
  • 540
  • 3
  • 5
  • 18

1 Answers1

0

I am not seeing anything wrong with the code. Here's what I did to test. I created two lists and two chars and then added the first char to the first list and the second char to the second list.

    let l0 = Lists()
    l0.listName = "list 0"

    let l1 = Lists()
    l1.listName = "list 1"

    let c0 = Characters()
    c0.character = "char 0 list 0"
    c0.charID = 0

    let c1 = Characters()
    c1.character = "char 1 list 1"
    c1.charID = 1

    l0.charIDs.append(c0)
    l1.charIDs.append(c1)

then write the data to Realm

do {
    let realm = try Realm()

    try! realm.write {
        realm.add(l0)
        realm.add(l1) 
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

and then checking realm using Realm Studio both the Characters and Lists were all written correctly and the Lists CharIDs property linked back to the character.

What may be causing issues is not the code, but how the objects are named. For example the Lists object isn't a list. It's a single object that contains a property of type lists with other properties that describe that particular list. Likewise, the Characters object isn't a bunch of characters, it's s single character with properties that describe that character.

suppose these objects are for an adventure type game and we want to keep track of which characters have visited a certain area within the game.

The first object is for a particular area within the game, which describes that area and contains a list property of the characters who have visited.

class AreaVisitedObject: Object {
   @obj dynamic var: area_name = ""
   @obj dynamic var: area_id = ""
   var charsWhoVisitedList = List<Character>()
   @objc dynamic var char_id = UUID().uuidString

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

and then the character object which describes each character

class CharacterObject: Object {
   @objc dynamic var char_name = ""
   @objc dynamic var char_id = UUID().uuidString

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

Note that I added a primary key object to the Character as it seems like you won't have duplicate chars so it will assure each is unique and they can be referenced by that uuid.

Hope that helps.

Jay
  • 34,438
  • 18
  • 52
  • 81