Parse Query Wrapping Error, works fine on simulator crashes on device

时间:2015-09-01 21:22:47

标签: ios swift parse-platform

The code below runs fine on the simulator then crashes on two devices and works on one device.

I'm also getting this:

function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded> of Swift.(_fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> ()).(closure #2)

Would it possibly have to do with bridging obj-C into my swift application? Any suggestions

var query = PFUser.query()
query?.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
var users = query?.findObjects()

//Loop through users
if let users = users as? [PFUser]{
    for user in users {
        //println(user.username)
        self.userArray.append(user.username!)
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell

    // Configure the cell...

    cell.textLabel?.text = userArray[indexPath.row]

    return cell
}

2 个答案:

答案 0 :(得分:1)

You want to perform the query in the background so the UI (main thread) stays responsive. Try the following:

if let currentUsername = PFUser.currentUser()?.username {
    var query = PFUser.query()!
    query.whereKey("username", notEqualTo: currentUsername)
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
        if (error == nil) {
            if let users = objects as? [PFUser] {
                for user in users {
                    self.userArray.append(user.username!)
                }
            }
        } else {
            // Log details of the failure
            println("query error: \(error) \(error!.userInfo!)")
        }
    }
}

答案 1 :(得分:0)

The first place I'd look is the forced unwrap of the optionals. Every one of those is asking for a crash -- when PFUser.currentUser() returns nil and user.username returns nil:

Try:

   var query = PFUser.query()
   if let query = query, 
   currentUser = PFUser.currentUser() as? PFUser, 
   currentUsername = currentUser.username {
      query.whereKey("username", notEqualTo: currentUsername)
      var users = query.findObjects()

      //Loop through users
      if let users = users as? [PFUser]{
           for user in users {
               //println(user.username)
              if let username = user.username {
                   self.userArray.append(username)
              }
          }
      }