Cannot add data to array inside read statement construct?

Hello all:


I am reading some data from an Azure SQL Server via an AppService, and I want to read the data into an array and then set a TableView to display the array of data. The code is pasted below, and I know the Names are being read correctly via the print statements but I cannot append the names to an array decalred outside the table.read scope. XCode tells me I must use "self", but the array is empty when the loop exits. If I declare the array in the construct it works fine, but then I cannot access the array outside the scope of the loop, and so cannot set the tableview to display the data.


Any and all help is appreciated!


@IBOutlet weak var namesTableView: UITableView!

var lastNames: [String] = []

override func viewDidLoad()

{

super.viewDidLoad()

let appDelegate = UIApplication.shared.delegate as! AppDelegate

contactTable = appDelegate.client?.table(withName: "Contact")

var iNameCount : Int = 0

contactTable?.read { (result, error) in

if let err = error {

print("ERROR ", err)

} else if let items = result?.items {

for item in items {

//print("LastName: ", NSString(data: item["lastName"] as! Data, encoding:String.Encoding.utf8.rawValue))

var thelastname: String?

thelastname = item["lastName"] as? String

let unwrappedln = thelastname!

print(unwrappedln)

self.lastNames.append(unwrappedln)

iNameCount += 1

}

print("Names count: " + String(iNameCount))

}

}

print(self.lastNames)

namesTableView.reloadData()

}

Replies

I do not know much about Azure SQL Server or AzureService, but the method `read` seems to be asynchronous.


You need to access the result of asynchronous call after the completion handler is called.


    override func viewDidLoad() {
        super.viewDidLoad()
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        contactTable = appDelegate.client?.table(withName: "Contact")
        var iNameCount : Int = 0
        contactTable?.read { (result, error) in
            if let err = error {
                print("ERROR ", err)
            } else if let items = result?.items {
                DispatchQueue.main.async {
                    for item in items {
                        //print("LastName: ", NSString(data: item["lastName"] as! Data, encoding:String.Encoding.utf8.rawValue))
                        var thelastname: String?
                        thelastname = item["lastName"] as? String
                        let unwrappedln = thelastname!
                        print(unwrappedln)
                        self.lastNames.append(unwrappedln)
                        iNameCount += 1
                    }
                    print("Names count: " + String(iNameCount))
                    print(self.lastNames)
                    self.namesTableView.reloadData()
                }
            }
        }
    }

but the array is empty when the loop exits


You are mistaking some things. In your code the completion handler `{ (result, error) in ... }` is executed after the read operation is completed.

If you put some lines after the `read`, the lines are exectuted before the completion of the read operation.