1

I tried to make a function to load data from Parse with SWIFT. Data is in the "crcl" className in Parse. I can't pass PFObject as AnyObject in my NSMutableArray "timeLineData". I have no code error but the app crash at launching.

What should i do, this is my code :

class TimelineTableViewController: UITableViewController {


var timeLineData:NSMutableArray = NSMutableArray ()


func loadData (){

    timeLineData.removeAllObjects()

    var findTimeLineData:PFQuery = PFQuery(className: "crcl")


    findTimeLineData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]! , error:NSError!)-> Void in
        if !error{

            for object:AnyObject in objects!   {

                self.timeLineData.addObject(object as PFObject)
            }


            self.tableView.reloadData()
        }
    }
}
1
  • 1
    What line is the code crashing on? Does the console give you any output when it crashes? Is self.tableView.reloadData() running on the main thread? Maybe try commenting out lines of code until it doesn't crash Commented Aug 26, 2014 at 22:23

2 Answers 2

2

Try to declare your timeLineData array like so:

var timeLineData = [PFObject]()

This tells Swift that timeLineData is an array containing PFObjects.

Then in your completion block use .append instead of .addObject like so:

self.timeLineData.append(object as PFObject)

That should do the trick!

Sign up to request clarification or add additional context in comments.

Comments

0

You are calling tableView.reloadData from within the block. You need to call dispatch async like so:

dispatch_async(dispatch_get_main_queue()) {
                    weakSelf!.tableView.reloadData()
                }

where weakSelf! is a weak reference to self declared sorta like:

weak var weakSelf : MasterViewController?

where MasterViewController is whatever controller you are in :)

1 Comment

weakSelf! could cause a crash; it should be checked with if weakSelf != nil first. Also, you can add [weak self] in at the beginning of the block and then use self directly instead of creating a whole new weakSelf variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.