Empty tableview results from Backendless Query, how to populate into table

298 Views Asked by At

Working on a questions and answer app similar to Quora. Moving from Parse to Backendless.

The app compiles, I see data in the output, but the tableview is empty. the data isnt appending the array? (using Swift 2, Xcode 7.2)

    import UIKit


class QuestionsTableViewController: UITableViewController {

    let backendless = Backendless.sharedInstance()
    var showAllQuestions = [String]()
    //var showAllQuestions = [String:AnyObject] = [Question]() as [String:AnyObject]
    var currentQuestion = [Question]()

    override func viewDidLoad() {
        super.viewDidLoad()

        Types.tryblock({ () -> Void in

            let startTime = NSDate()
            let query = BackendlessDataQuery()
            var allQuestions = self.backendless.persistenceService.of(Question.ofClass()).find(query)
            print("Total Questions in the Backendless starage - \(allQuestions.totalObjects)")

            let bc1 : BackendlessCollection = allQuestions as BackendlessCollection
            for order : Question in bc1.data as! [Question] {

                self.showAllQuestions.appendContentsOf(self.showAllQuestions)
                self.tableView.reloadData()

            }
            var size = allQuestions.getCurrentPage().count
            while size > 0 {
                print("Loaded \(allQuestions) questions in the current page")
                allQuestions = allQuestions.nextPage()
                size = allQuestions.getCurrentPage().count
            }

            print("Total time (ms) - \(1000*NSDate().timeIntervalSinceDate(startTime))")
            },

            catchblock: { (exception) -> Void in
                print("Server reported an error: \(exception as! Fault)")
            }
        )

    }

   override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

       return showAllQuestions.count
    }

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("questionCell",
            forIndexPath: indexPath) as! QuestionsTableViewCell


        cell.userNameLabel.text = showAllQuestions[indexPath.row]
        cell.questionLabel?.text = showAllQuestions[indexPath.row]


        return cell
    }

This outputs this:

Total Questions in the Backendless starage - 8

Loaded <BackendlessCollection> -> type: BloQuery2.Question, offset: 0, pageSize: 100, totalObjects: 8, data: ( "<BloQuery2.Question: 0x7faab60091f0>", "<BloQuery2.Question: 0x7faab3e1f280>", "<BloQuery2.Question: 0x7faab600aae0>", "<BloQuery2.Question: 0x7faab600bbf0>", "<BloQuery2.Question: 0x7faab600cd80>", "<BloQuery2.Question: 0x7faab600ddf0>", "<BloQuery2.Question: 0x7faab600eeb0>", "<BloQuery2.Question: 0x7faab600ffa0>" ), query: <BackendlessDataQuery> -> properties: (null), whereClause: (null), queryOptions: <QueryOptions> -> { offset = 0; pageSize = 100; related = ( ); } questions in the current page Total time (ms) - 1122.19899892807

2

There are 2 best solutions below

5
On

What kind of data is in the "self.allQuestions"? It is not coming as String. It is of type BloQuery2.Question Please change the type through any function to get the Value as String from "BloQuery2.Question".

7
On

Please change the code from

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

  let blocQuery:BloQuery2 = showAllQuestions[indexPath.row] as BloQuery2
    let onequestion:Question = blockQuery.Question as Question
    cell.userNameLabel.text = onequestion.userName as? String
    cell.questionLabel?.text = onequestion.question as String