0

The problem is that I manage scrollView with lots of tiles in it. Each visible tile display image loaded from URL or (after first URL load) cached file in background. Invisible tiles recycles (set new frame and redraw).

Image load depends on tile position.

With long range scroll there is multiple redraw called for each tile: each tile loads (and display) different image several times before display the correct one.

So problem is to cancel all previously added operations for tile before add new.

I subclass NSInvocationOperation just to contain context object to detect operation attached to and before add new operation I canceling all operation for same tile:

 -(void)loadWithColler:(TileView *)coller {    
    if (queue == nil) {
        queue = [NSOperationQueue new];
    }

    NSInvocationOperationWithContext *loadImageOp = [NSInvocationOperationWithContext alloc];
    [loadImageOp initWithTarget:self selector:@selector(loadImage:) object:loadImageOp];
    [loadImageOp setContext:coller];

    [queue setSuspended:YES];
    NSArray *opers = [queue operations];
    for (NSInvocationOperationWithContext *nextOperation in opers) {

        if ([nextOperation context] == coller) {
            [nextOperation cancel];
        }

    }

    [queue addOperation:loadImageOp]; 
    [queue setSuspended:NO];    
    [loadImageOp release];
}

And in operation itself I check isCancelled:

    -(void)loadImage:(NSInvocationOperationWithContext *)operation {

        if (operation.isCancelled) return;

        TileView *coller = [operation context];

        /* TRY TO GET FILE FROM CACHE */    
        if (operation.isCancelled) return;

        if (data) {

            /* INIT WITH DATA IF LOADED */

        } else {
            /* LOAD FILE FROM URL AND CACHE IT */
        }

        if (operation.isCancelled) return;

        NSInvocationOperation *setImageOp = [[NSInvocationOperation alloc] initWithTarget:coller selector:@selector(setImage:) object:cachedImage];
        [[NSOperationQueue mainQueue] addOperation:setImageOp];
        [setImageOp release];

    }

But it is do nothing. Some times early returns works but tiles still load many images before the correct one.

So how could I success? And could this lots of unneeded operations cause delays on main thread when scrolling? (Because delays are exists and I do not know why...all load in background..)

Update:

With NSLog: isCancelled while executing: > cancel loadImage method for: >

So canceling work.

Now I save reference to last operation in TileView object and perform setImage operation only if invoked operation is equal to TileView operation.

Doesn't make any difference...

Looks like there IS number of operations to load different images to one tile invoked one after another.

Any another suggestions?

For clearance:

There is singleton DataLoader (all code from it). And all tiles has call to it in drowRect:

[[DataLoader sharedDataLoader] loadWithColler:self];

Update:

NSInvocationOperation subclass:

@interface NSInvocationOperationWithContext : NSInvocationOperation {
    id context;
}

@property (nonatomic,retain,readwrite) id context;

@end


@implementation NSInvocationOperationWithContext

@synthesize context;


- (void)dealloc
{
    [context release];
    [super dealloc];
}
@end

Thanks a lot for any help!

SOLUTION:

From answer below: need to subclass from NSOperation

As I subclass NSOperation and put all loadImage: code into it "main" method (just move all code here and nothing else) and all work just perfect!

As about scroll delaying: it occurs cause loading images to UIImageView (it takes long time because of decompress and rasterize (as I understood).

So better way is to use CATiledLayer. It loads data in background and do it much faster.

Gusev Andrey
  • 446
  • 5
  • 23

2 Answers2

1

The way that NSOperationQueue works with respect to "setSuspended" is that it won't start to run newly added NSOperations added to it after that point, and won't start to run any that are currently in it that haven't started running yet. Are you sure your operations you're trying to cancel haven't already started yet?

Also - does your NSOperation subclass correctly deal with Key Value Observing? Concurrent Queue subclassed NSOperations have to call willChangeValueForKey and didChangeValueForKey for some properties here - but doesn't look like that's the issue as your queue doesn't set isConcurrent. Just FYI if you go that route.

Scott Corscadden
  • 2,831
  • 1
  • 25
  • 43
  • I'm not sure that it haven't started. Exactly for this I check isCancelled inside target's method to cancel operation that already started. Subclass only adds context variable and do nothing more. It still needs to redefine all of NSOperation's staff? And yes. I do not use concurrent, it can help if I'll use? – Gusev Andrey Mar 17 '12 at 12:29
  • Sure - but isCancelled is a KVO property - add an NSLog() statement inside your subclass that prints out an ID or something when the method starts, and you can correlate that against your isCancelled check. – Scott Corscadden Mar 17 '12 at 12:32
  • Don't clearly understand... Add NSLog to loadImage:? For what? – Gusev Andrey Mar 17 '12 at 13:03
  • Introspecting GCD based state with threads you don't control is tricky. Adding a log line to the NSOperation subclass you have created upon start/cancel so that you know what exactly is happening across the threads can help. – Scott Corscadden Mar 17 '12 at 14:03
1

The delays on main thread is due to a the mode of the runloop while you scroll. I suggest you to watch the WWDC2011 networking app sessions. I don't know if it is fine to subclass an NSInvocationOperation that is a concrete subclass of NSOperation. I will subclass NSOperation instead. For my experience if you like to avoid sluggish scrolling, you should create NSOperation subclasses that load their main on a specific thread for networking operation (you must create it). There is a wonderful sample code from apple https://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html

ricardopereira
  • 11,118
  • 5
  • 63
  • 81
Andrea
  • 26,120
  • 10
  • 85
  • 131
  • But sluggish scrolling occurs even if data load from cached files and not use network at all. All data loads in specific thread managed NSOperationQueue. Can it help if I will create another new threads from NSOperation subclass? – Gusev Andrey Mar 17 '12 at 12:44
  • Probably there are too many threads... try to set the max number of concurrent operation in the queue, see what's happen – Andrea Mar 17 '12 at 18:09
  • Forgot also to say to try too check performance using core animation instruments and time profiler to see if there is any long operation. – Andrea Mar 17 '12 at 18:11