1
votes

Scenario is like this--

In my app there is an Scroll View with many instances of

MyCustomImageDownloaderController(containing imageViews where images are to be assigned after downloading) .

As per our requirement, an image has to be downloaded as we move on to a page.

Scroll View + (MyCustomImageDownloaderController1, MyCustomImageDownloaderController2, MyCustomImageDownloaderController3.... etc)

Let's say i am scrolling on it,
i reached to page 1 --> image for it should start downloading
i reached to page 2 --> image for it should start downloading...so on

and if i am on page 3 and images for previous pages if not been dowloaded, they should stop downloading.

So i tried it with using threads.. on API..

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender{

    Step 1) calculated currentPageNumber

    Step 2) started thread for downloading image with url for this currentPage
    //startBackGroundThreadForPlaceImage:(NSURL *) url

    Step 3)stopped thread for previous page , if that is still running

   }

Now My MyCustomImageDownloaderController is as

-(void) startBackGroundThreadForPlaceImage:(NSURL *) url{

if(isImageDownloaded == NO){

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    //[self performSelectorInBackground:@selector(loadImageInBackground:)       withObject:imageUrl];
    myThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageInBackground:) object:imageUrl]; 
    //[NSThread detachNewThreadSelector:@selector(loadImageInBackground:) toTarget:self withObject:imageUrl];
    [myThread start]; 
    NSLog(@"The current thread is %@ ", [[NSThread currentThread] name]);
    [pool release]; 
}

}

NOW Here selector does the work of loading image and assigning to image view

Now Stopping the thread

-(void) stopBackgroundThread{
[myThread cancel];

//[[NSThread currentThread] cancel];

//if([[NSThread currentThread] isCancelled]) {
    //[NSThread exit];
//}
    [NSThread exit];

}

-(BOOL) isThreadRunning{
return [myThread isExecuting];

}

So i tried a lot of things, but could not Stop the thread in between..

Basically once instantiated thread using any of three methods

1) perform Selector in BackGround

2) NSThread detach new thread

3) NSThread alloc..init with..

In first 2 methods how to get the instance of the newly created thread, so that i could stoop it,

as NSThread currentThread doest not give that

in Method 3,

myThread = [[NSThread alloc] initWithTarget:self selector:@selector(loadImageInBackground:) object:imageUrl]; 

when i tried

[myThread cancel];

It did not cancel that thread,,

When i tried

[NSThread exit];

it hangs on current screen,,,,i guess it has stopped the main thread

Please help me

Thanks in Advance*strong text*

1

1 Answers

3
votes

It's generally better to ask the thread to stop, rather than forcing it, which should be considered a last resort. You should, therefore, frequently check a 'stop flag' and when this gets set, terminate the current thread (by simply exiting the method, in this case). You just need to provide a property on the class the thread is operating on so callers can ask the thread to stop.

It's no different in C++ or Java.