Daisy R.
Daisy R.

Reputation: 633

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

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
}

Upvotes: 1

Views: 79

Answers (2)

Russell
Russell

Reputation: 3089

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!)")
        }
    }
}

Upvotes: 1

algal
algal

Reputation: 28094

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)
              }
          }
      }

Upvotes: 0

Related Questions