简体   繁体   English

PFFile返回nil可选值-Swift 1.2

[英]PFFile returning nil Optional Value - Swift 1.2

I have a problem on fetching Parse image files (PFFile). 我在获取解析图像文件(PFFile)时遇到问题。

   Usersclass { 
       var resultsUsernameArray = [String]() 
       var imageFiles = [PFFile]()
  }


  override func viewDidAppear(animated: Bool) {

    let predicate = NSPredicate(format: "username != '"+userName+"'")
    var query = PFQuery(className: "_User", predicate: predicate)


    if let objects = query.findObjects() {
        for object in objects {

            var user:PFUser = object as! PFUser
            self.resultsUsernameArray.append(user.username!)

            if object["photo"] != nil {

                self.imageFiles.append(object["photo"] as! PFFile)
               // this is where I get error

                self.resultsTable.reloadData()

            }
        }
    }

}

It used to work fine but with Swift 1.2, it seems like there is a problem. 它曾经可以正常工作,但是在Swift 1.2中,似乎存在问题。 The error I am getting is: " Warning: A long-running operation is being executed on the main thread. Break on warnBlockingOperationOnMainThread() to debug. fatal error: unexpectedly found nil while unwrapping an Optional value (lldb) " 我得到的错误是:“ 警告:在主线程上正在执行长时间运行的操作。中断warnBlockingOperationOnMainThread()进行调试。致命错误:展开一个可选值(lldb)时意外发现nil

You're executing a fetch on the main queue, so I'm surprised this worked. 您正在主队列上执行访存,所以我对此工作感到惊讶。 The warning is telling you that you are trying to perform a long running operation on the main thread. 该警告告诉您您正在尝试在主线程上执行长时间运行的操作。 You should change your findObjects() call to a call in the background, such as findObjectsInBackground , something like this: 您应该将findObjects()调用更改为后台调用,例如findObjectsInBackground ,如下所示:

query.findObjectsInBackgroundWithBlock({ (NSArray array, NSError error) -> Void in
    for object in objects {

        var user:PFUser = object as! PFUser
        self.resultsUsernameArray.append(user.username!)

        if object.objectForKey("photo") != nil {

            self.imageFiles.append(object.objectForKey("photo") as! PFFile)
           // this is where I get error

            self.resultsTable.reloadData()

        }
    }            
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM