4

I have a Posts class that I am querying in a UITableViewController using Parse as my backend.

In my viewDidAppear I call my loadData() function. And then I have a var refresher = UIRefreshControl() that is responsible for my refresh() function.

After a few time reloading the data I get a a fatal error: Array index out of range and the following line of code highlighted which is in my cellForRowAtIndexPath.

enter image description here

What was interesting is when I printed out what was in my index path using println(drive), all the posts were there. But then there were instances where some posts appeared twice then the app would crash.

timelineData.removeAll(keepCapacity: false)

I thought that having this should clear everything so I am not sure why this is happening.

Here is my code for my refresh function.

func refresh()
{

    println("refresh table from pull")

    timelineData.removeAll(keepCapacity: false)

    var findItemData:PFQuery = PFQuery(className:"Posts")

    findItemData.addDescendingOrder("createdAt")

    findItemData.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]? , error:NSError?) -> Void in
        if error == nil
        {
            self.timelineData = objects as! [PFObject]
            self.newsFeedTableView.reloadData()

        }

    }
    self.refresher.endRefreshing()


}

I tried using Parse's query.cachePolicy but that didn't matter because the crash kept happening. https://parse.com/docs/ios/guide#queries-querying-the-local-datastore

I also thought it was because I have Parse.enableLocalDatastore() but still no luck.

I do call my other function loadData in my viewDidAppear as mentioned earlier, not sure if this might be the problem, but I don't know how to check for data when there is an update. Still not sure if this is the source of the problem.

EDIT 1

I have attached my timelinedata count in several functions. Second image is when I print the count in my cellForRowIndexPath enter image description hereenter image description here

kareem
  • 903
  • 1
  • 14
  • 36
  • You should print the array's bounds (count) a couple times at multiple locations in your code. – LinusGeffarth Jun 21 '15 at 23:16
  • so println(timelinedata.count) is what you mean? @LinusG. – kareem Jun 21 '15 at 23:19
  • Yeah. And then post the output in your post. – LinusGeffarth Jun 21 '15 at 23:20
  • @LinusG. I added the screen shots of the output, there was in fact 9 test posts – kareem Jun 21 '15 at 23:33
  • Have you tried to put timelineData.removeAll(keepCapacity: false) into the block? – Miknash Jun 21 '15 at 23:36
  • @NickCatib nope i havnt, i will try it, the reason i havnt is because several of my other apps have never had this problem and ive been getting the data the exact same way, i will tell u what happens trying it now – kareem Jun 21 '15 at 23:40
  • @NickCatib so after putting timelineData.removeAll(keepCapacity: false) into the block and testing it across different devices in simulator i havnt been able to replicate the issue, go ahead and submit it as an answer and after i test it more thoroughly then i can make it the accepted answer, not sure if it will hold through but its been a while and usually i get the error much more quickly – kareem Jun 21 '15 at 23:50
  • this - `self.refresher.endRefreshing()` should be inside the closure for `findObjectsInBackgroundWithBlock` as the refresh hasn't completed until this closure completes – Paulw11 Jun 22 '15 at 00:28
  • @Paulw11 so self.refresher.endRefreshing() should be in the last bracket of the block? – kareem Jun 22 '15 at 00:33
  • Yes, that way it will be called when the asynchronous fetch is complete – Paulw11 Jun 22 '15 at 03:14
  • @Paulw11 thanks i have made the fix – kareem Jun 23 '15 at 07:16

1 Answers1

3

Try to:

findItemData.findObjectsInBackgroundWithBlock{
    (objects:[AnyObject]? , error:NSError?) -> Void in
    if error == nil
    {
        timelineData.removeAll(keepCapacity: false)
        self.timelineData = objects as! [PFObject]
        self.newsFeedTableView.reloadData()
    }
} 

It could happen that you have inconsistent data before you actually populate your list. This way you will have your data if some kind of error occurs, so you are safe from that point as well.

Miknash
  • 7,888
  • 3
  • 34
  • 46