SpikerJG
SpikerJG

Reputation: 41

Defining a Realm Database Structure (Swift)

I am in the process of creating an application that will display a list of stocks that a user saves in a tableView. It will also allow the user to add or remove items from their favorites. I need help defining the database structure and setting up for the adding and constant updating of the user's favorite stocks.

I currently have a StockData struct that works with my tableView and a button for adding to the user's list:

struct StockData {
    let currentPrice: Double    
    // meta data
    let name: String
    let ticker: String
    let interval: String
    let lastRefreshed: String
    let change: Double 
}

// In an actual ViewController
@IBAction func addButtonClicked(_ sender: Any) {
    print("Add clicked")
    // Handle adding the item to the user's list
}

Now as far as my current realm model is concerned, I have:

class User: Object {
    @objc dynamic var name = ""
    @objc dynamic var id = ""

    var stockFavs = List<StockItem>()
}

class StockItem: Object {
    @objc dynamic var currentPrice: Double = 0.0
    // meta data
    @objc dynamic var name = ""
    @objc dynamic var ticker = ""
    @objc dynamic var interval = ""
    @objc dynamic var lastRefreshed = ""
    @objc dynamic var change: Double = 0.0
}

// Setting up the user and creating test values
        let newUser = User()
        newUser.name = "John Smith"
        newUser.id = "coolId123"

        var stockArr = List<StockItem>()
        for i in 0..<12 {
            let item = StockItem()
            item.name = "Microsoft Corporation"
            item.change = -3.55
            item.currentPrice = 123.45
            item.interval = "1day"
            item.lastRefreshed = "Now"
            item.ticker = "MSFT"
            stockArr.append(item)
        }

        newUser.stockFavs = stockArr

        try! realm.write {
            realm.add(newUser)
        }

So far, I have been able to create a user object for the current user of the device and add test values, but I am unsure how to implement constant realm updating (the method would have self.tableView.reloadData(), but apart from that, I'm clueless) in conjunction with the ability to add StockItem's to the user's array.

Any help would be much appreciated!

Upvotes: 0

Views: 226

Answers (1)

Cody Weaver
Cody Weaver

Reputation: 4886

You use a function for every time you want to add to the database.

let button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100)
button.addTarget(self, action: #selector(add), for: .touchUpInside)

func add() {
  let currentData = [MyFakeData1, MyFakeData2, etc.]
  try! realm.write {
    realm.add(currentData)
  }
  // You need to get the updates in the database
  // You could have an array in your model that you update and then append all
  // the new items to it and only do database lookups when you restart the 
  // device
  update()
}

func update() {
  let realm = try! Realm()
  var newArray = realm.objects(StockItem.self)
  myViewController.modelArray = newArray
  table.reloadData()
}

Upvotes: 1

Related Questions