Skip to content

Instantly share code, notes, and snippets.

@mthongvanh
Last active August 29, 2015 14:10
Show Gist options
  • Save mthongvanh/5e315c0ac68f2ef986b8 to your computer and use it in GitHub Desktop.
Save mthongvanh/5e315c0ac68f2ef986b8 to your computer and use it in GitHub Desktop.
via #iosdev on irc.freenode.net: With the Parse SDK, a user had trouble displaying images that downloaded successfully.
// Problem: Collection View data does not appear after calling [UICollectionView reloadData] in [PFQuery findObjectsInBackgroundBlock:]
// Symptoms: The correct number returns in numberOfItems: but cellForItem: is never called
// Solution: Try performing collection view updates manually via performBatchUpdates: or reloadItemsAtIndexPaths: or reloadSectionsAtIndexSet:
- (void)queryForPhoto {
NSLog(@"%s", __PRETTY_FUNCTION__);
PFQuery *query = [PFQuery queryWithClassName:@"Image"];
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];
[query orderByAscending:@"createdAt"];
[query setCachePolicy:kPFCachePolicyNetworkOnly];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// success
NSLog(@"retrieved %lu photos.", (unsigned long)objects.count);
self.photoArray = [NSMutableArray arrayWithCapacity:objects.count];
for (PFObject *object in objects) {
PFFile *thumbnail = [object objectForKey:@"thumbnail"];
[thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
[self.photoArray addObject:[UIImage imageWithData:data]];
// [self.collectionView reloadData];
[self reloadCollection:self.collectionView];
NSLog(@"photoArray array size: %lu", (unsigned long)[self.photoArray count]);
} else {
NSLog(@"error: %@ %@", error, [error userInfo]);
}
}];
}
} else {
// details of the failure
NSLog(@"error: %@ %@", error, [error userInfo]);
}
}];
}
- (void)reloadCollection:(UICollectionView*)collectionView {
[UIView setAnimationsEnabled:NO];
[collectionView performBatchUpdates:^{
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} completion:^(BOOL completed){
[UIView setAnimationsEnabled:YES];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment