Unable to load data from Parse Server with error =( Error?) nil none

162 Views Asked by At

I'm unable to load data from the server as I'm getting an error - error =( Error?) nil none. Before the error it sees the PFObject's 4 values. Then download and beats. And pointing to a line - let detailPrognozS = object["detailPrognozS"] as! String. I'm not sure what is wrong?

func detailObject() {

    let query = PFQuery(className: "soccer")
    query.findObjectsInBackground { (objects:[PFObject]?, error:Error?) in
        if error == nil {
            for object in objects! {
                let detailPrognozS  = object["detailPrognozS"] as! String
                let detailTitleS = object["detailTitleS"] as! String
                let detailTextS = object["detailTextS"] as! String
                let imageDetail = object["detailImageS"] as! PFFile

                DispatchQueue.main.async { [unowned self] in
                    self.prognozDetail.text = detailPrognozS
                    self.textView.text = detailTextS
                    self.titleDetail.text = detailTitleS
                }

                imageDetail.getDataInBackground(block: { (data:Data?, error:Error?) in
                    if error == nil {
                        DispatchQueue.main.async { [unowned self] in
                            let imageData = data
                            self.imageDetail.image = UIImage(data: imageData!)
                        }
                    }
                })

            }
        }
    }

}
2

There are 2 best solutions below

0
On
let query = PFQuery(className: "soccer")
            query.findObjectsInBackground { (objects: [PFObject]?, error) -> Void in
                if error == nil {
                    for object in objects! {



                        self.prognozDetail.text = object["detailPrognozS"] as? String
                        self.textView.text = object["detailTextS"] as? String
                        self.titleDetail.text = object["detailTitleS"] as? String
                        let imageDetail = object["detailImageS"] as? PFFile


                        imageDetail?.getDataInBackground(block: { (data:Data?, error:Error?) in
                            if error == nil {

                                    if  let imageData = data {

                                        self.imageDetail.image = UIImage(data: imageData)

                                        }

                            }

                        })
                    }
                }
            }
3
On

Please could you be a little more specific. I would imagine what is happening is that when you're going off getting the data in the background it is triggering the next for loop to run and not giving it a chance to return the data.

You should really clean up this code though. Shouldn't be setting uilabels text within a network call, all it could be doing is returning a completion handler with an object and and error status or nil. Please look into some MVC patterns I think this will help. Short term fix would be to potentially add a "let dispatchGroup = DispatchGroup()" and set when it enters and leaves. Hope this helps.

https://developer.apple.com/documentation/dispatch/dispatchgroup https://www.raywenderlich.com/160651/design-patterns-ios-using-swift-part-12