Search code examples
iosuitableviewswiftfmdb

How to save a resultSet and show all data in TableView


I am using FMDB for my application and now I want to show my resultSet in TableView. Here is some code-

    func getUser() {

    sharedInstance.database!.open()

    var resultSet: FMResultSet! = sharedInstance.database!.executeQuery("SELECT * FROM user_info", withArgumentsInArray: nil)

    if(resultSet != nil)  {
        while resultSet.next() {
           .
           . 
           .

        }
    }

Now here are many users in resultSet.Where and How can I put the results from resultSet so that I can show them in my TableView afterwards?


Solution

  • As you are iterating through the FMResultSet, it moves to the next row so you can do the below to save an array of your User objects

        let resultSet:FMResultSet! = FMResultSet()
        if (resultSet != nil) {
            var users:Array<User> = Array<User>()
            while (resultSet.next()) {
                 let user:User = User()
                 user.Id = resultSet.intForColumn("user_id")
                 user.name = resultSet.stringForColumn("user_name")
    
                 users.append(user)
            }
       }
    

    // Now you have your users array here and you can reload its data into your UITableView as you want.