0

Please refer to the answer of FMDatabaseQueue How To Return A Value. It said the function just return the value at the end of the function. But my code in swift doesn't work this way. Here is my code:

class func isSomethingRight()-> Bool {

    var ret : Bool = false

    self.queue.inDatabase({ database in
        let sql = NSString(format: "SELECT * FROM Table WHERE tableId = '1001'")
        if let resultSet = database.executeQuery(sql, withArgumentsInArray:nil) {                

            if resultSet.next() {
                ret = true
            }
        }
    })        
    return ret
}

In my code, the return ret return first, then the ret = true inside block (closure) called later. What could be wrong in my code? (Actually the exactly same code works in my another project in Swift).

Community
  • 1
  • 1
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
  • It all looks right. You're sure that your block is being called later and it's not just returning false correctly? There's a good chance it would cause a crash trying to set `ret` after the method has ended. – pwightman Nov 30 '14 at 15:41
  • Yes, I'm sure that my block was called later after the function returned. – Bagusflyer Nov 30 '14 at 16:08

0 Answers0