1

so i'm fairly new to Xcode and C# so this may be a silly question, but none-the-less it has had me tearing my hair out for 3 days now.

Ok, so I am using Parse to fetch and save data, images, etc. I want to display a UITableView with the PFFile images. This bit I have managed.

The issue I have is the the scrolling performance is bad because i'm not caching the PFFile images that come in. I have no idea how to do this and any searches i make don't take into account using parse.

below is my code that puts the initial data into an array:

-(void) retrieveFromParse {

PFQuery *retrieveContent = [PFQuery queryWithClassName:@"NewsFeed"];
[retrieveContent orderByDescending:@"createdAt"];
//[retrieveContent unpinInBackground];
//retrieveContent.cachePolicy = kPFCachePolicyCacheElseNetwork;


[retrieveContent findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {


    if(!error){
        mainArray = [[NSArray alloc] initWithArray:objects];

    }

    [tableView reloadData];


}];

}

This code then gets the images:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"MyCell";

TableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.height /1.99;
cell.profilePicture.layer.masksToBounds = YES;
cell.profilePicture.layer.borderWidth = 0;


PFObject *imageObject = [mainArray objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"image"];

[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if(!error){

        NSLog(@"%@",data);

        cell.myImage.image = [UIImage imageWithData:data];

    }

}];


return cell;

}

Now, you'll see i have commented out the cachePolicy because every time i use it, the thread breaks.

If someone could please alter my code or provide me with an example project using my code, to take into account caching either array and/or PFFile images, that would save me from going bald! :)

Many thanks in advance.

James Mann
  • 61
  • 1
  • 7

1 Answers1

0

Did you tried retrieving the images in the viewdidload function instead of the cellForRowAtIndexPath?

I had the same problem before and I solved almost 90% of this problem by doing the following:

1- in the viewdidload, i wrote 2 queries: 1 for retrieving the data, and the other one to retrieve the images and store them in an array. 2- in the cellForRowAtIndexPath, i just added the images i got in the view did load to my cell.

this is what I did but unfortunately I'm still having a problem. The problem is that the images are not retrieved in the same order of retrieving the data "the image is not displayed next to the correct data :/". I'm trying to solve this issue and if i did i will post the solution :)

Hope my Solution will help you. good luck ;)