Reputation: 33654
Currently I am doing the following to iterate on a NSMutableIndexSet:
if ([indexSet isNotNull] && [indexSet count] > 0){
__weak PNRHighlightViewController *weakSelf = self;
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
[weakSelf.collectionView reloadItemsAtIndexPaths:[NSArray arrayWithObject:indexPath]];
}
}];
}
I wanted to generate a NSIndexPath array and then reload the whole collectionView at those index paths. So basically I want to call the reload after the block is complete. How can I do so?
Upvotes: 0
Views: 1760
Reputation: 9464
If a method doesn't ask for a dispatch queue or NSOperationQueue
for a block argument to run on, and the docs don't say otherwise, you can generally assume that it executes blocks synchronously. Blocks do not imply parallelism, and the documentation will tell you when a block is in fact run asynchronously.
NSNotificationCenter
's block observer method would be an example of a method that executes a block asynchronously. And in that instance it asks for an NSOperationQueue
.
Upvotes: 1
Reputation: 119242
Build the array during the block. The iteration is performed synchronously (so you don't really need to worry about the weak self either):
if ([indexSet isNotNull] && [indexSet count] > 0){
__weak PNRHighlightViewController *weakSelf = self;
NSMutableArray *indexPaths = [NSMutableArray new]; // Create your array
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
[indexPaths addObject:indexPath];
}
}];
[self.collectionView reloadItemsAtIndexPaths:indexPaths];
}
Upvotes: 0
Reputation: 23278
One way to do this would be,
[indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
if ([[self.highlightedItems_ objectAtIndex:idx] isNotNull]){
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection: [weakSelf.collectionView numberOfSections]-1];
//store the indexPaths in an array or so
}
if (([indexSet count] - 1) == idx) { //or ([self.highlightedItems_ count] - 1)
//reload the collection view using the above array
}
}];
}
Upvotes: 3