1

I have created a class as below:

import Foundation
import RealmSwift

class StockCellContent: Object{

    @objc dynamic var StockName: String = ""
    @objc dynamic var image: String = ""
    @objc dynamic var requiredStockPrice: Double = 0.0000
    @objc dynamic var requiredPriceUpOrDown: String = ""
    @objc dynamic var StockPrice: Double = 0.0000
    //var parentCategory = LinkingObjects(fromType: Category.self, property: "StockCellContent")

}

then i have created a object of that class and called a function which gets the data through an api and saves it in the object. Then i add the object to an array to be printed on the UITableView. The UITableView prints the values perfectly fine but when i try to pass the same object anywhere else or even print it to the console the data becomes nil or the default value with which it has been initialised. I don't understand what i am doing wrong here.

the function is as below:

func dataReceived(data: String) {
    self.stockNames.append(data)
    self.stockKeyword = data
    //var stockData = StockCellContent()
    //var stockDataTest = StockObjectTest()
    let params = ["function" : "GLOBAL_QUOTE", "symbol" : stockKeyword, "apikey" : stockApiKey]
    let stockData = getStockPriceFromJSON(url: stockApiURL, parameters: params)
    //print(stockData.StockPrice)

    print(stockData.StockPrice)
    self.stockArray.append(stockData)

    //print(data)
    //print(stockData.StockName)
    //print(stockData.StockPrice)
    //print(self.stockArray[0].StockPrice)
    //self.stockCellContents = stockData
    //self.stockList.append(newCategory)
    self.save(stockCellContent: stockData)

    self.stockUpdateFlag = true
    //updateTable()
    //tableView.reloadData()
}

The print of stockData.StockPrice shows 0.0 even stockData.StockName shows empty

Bhavesh Nayi
  • 705
  • 4
  • 15
Kevin John
  • 23
  • 3
  • Is `getStockPriceFromJSON` async? If so, maybe you should fill `stockData` inside its closure after the response has received. – M Reza Apr 27 '19 at 03:14
  • Please see [Returning data from async call in Swift function](https://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function?r=SearchResults&s=1|67.3104) – rmaddy Apr 27 '19 at 03:36
  • No. It's not async. And honestly, I do not know how to make it even if I wanted to. – Kevin John Apr 27 '19 at 05:18
  • Have you looked in the debugger? Is it possible that your assumption "The UITableView prints the values perfectly fine" is somehow wrong (using default values or getting the values from someplace else)? Have you checked the getStockPriceFromJSON (for example by intentionally crashing it, if the value is correct)? – Gerriet Apr 27 '19 at 06:43
  • 1
    Please add `getStockPriceFromJSON`. Most likely it is indeed asynchronous. – vadian Apr 27 '19 at 06:53
  • I figured out the problem. The getStockPriceFromJSON method which tries to get the data from the api takes too long to return the data and the rest of the code is been executed even before the data is returned and set to the object variable. So the method was returning the empty first and then the data was then getting set. I realised it when i tried to print two tests in the if else case inside alamofire closure and just before the return statement. The line before the return statement got executed first and then the one which was inside the if else. – Kevin John Apr 27 '19 at 20:28
  • This still did not explain how the table view used to get the data properly. The default valued were 0, prior to me setting it. – Kevin John Apr 27 '19 at 20:30

0 Answers0