Difference between NSOperationQueue and NSThread in iOS4?
Difference between NSOperationQueue and NSThread in iOS4?
- Subject: Difference between NSOperationQueue and NSThread in iOS4?
- From: Scott Andrew <email@hidden>
- Date: Tue, 24 Aug 2010 11:42:55 -0700
I have a question that I have been researching but can't find an answer for.
I have some iOS 3.2 code using NSOperation this doesn't work using NSOperation but works using NSThread withe detatch thread in iOS4 with the desired effect. My code is basically to create and generate pages for my paged scrollview in the background. Its basically a play on the WWDC picture scroller demo. I however have some almost full screen views we prepare in the background. The code look like:
-(void) tilePages {
@synchronized(self)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
CGRect visibleBounds = infoCardScroller.bounds;
NSInteger firstNeededIndex = floorf(CGRectGetMinX(visibleBounds)/CGRectGetWidth(visibleBounds));
NSInteger lastNeededIndex = floorf((CGRectGetMaxX(visibleBounds)-1)/CGRectGetWidth(visibleBounds));
currentIndex = firstNeededIndex;
firstNeededIndex = MAX(firstNeededIndex-1, 0);
lastNeededIndex = MIN(lastNeededIndex+1, [songs count] - 1);
// recycle no longer visible pages
for (HVInfoCardContainer* infoCard in visibleViews) {
if (infoCard.index < firstNeededIndex || infoCard.index > lastNeededIndex) {
[recycledViews addObject:infoCard];
[infoCard removeFromSuperview];
}
}
[visibleViews minusSet:recycledViews];
// add missing pages.
for (int index = firstNeededIndex; index <= lastNeededIndex; index++) {
if (![self isDisplayingContainerForIndex:index]) {
HVInfoCardContainer* container = [self dequeueRecycledContainer];
if (container == nil)
container = [[[HVInfoCardContainer alloc] init] autorelease];
[self configureContainer:container forIndex:index];
[infoCardScroller addSubview:container];
[visibleViews addObject:container];
}
}
[pool release];
}
;
}
I cached 3 views at most. I basically have the following when the view scrolls:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
[[NVGlobalQueue sharedNVGlobalQueue] cancelAllOperations];
[[NVGlobalQueue sharedNVGlobalQueue] addOperation:[[[HVInfoCardTileOperation alloc] initWithScroller:self] autorelease]];
}
My operations's main is:
-(void) main {
if (!self.isCancelled) {
[self setQueuePriority:NSOperationQueuePriorityVeryHigh];
[scrollerController tilePages];
}
}
} }
On iOS 3.2 i get what i expect. The third view is created and rendered as we scroll to the 2nd one. So i have 3 views in the my scroller full rendered as I scroll to the 2nd one. However, on iOS4 this isn't working I don't get my 3rd one for a few seconds after its scrolled too. However if i change to:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[NSThread detachNewThreadSelector:@selector(tilePages) toTarget:self withObject:nil];
}
This works great and scrolls as smooth as butter. I don't mind doing this since there is a check to see if we have to actually create the view (could probably make the tileView call even smarter). But why would this not work over NSOperation on iOS4 but work in 3.2? Going to the main thread isn't an options as it chunks the scroll And no removing the cancelAllOperations doesn't help.
Scott Andrew_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden