Problem with NSThread on iOS

I have an application that uploads several thumbnails to UIScrollVIEW. This is a lengthy operation, and in order not to block the display of the rest of the user interface, I run it in a separate thread. This works the first time the application starts, but later a new set of images is loaded into UIScrollView. When I disconnect the stream, the second time the application crashes (sometimes). Code follows:

// this call is in a separate method
[NSThread detachNewThreadSelector:@selector(addThumbnailsToScrollView) toTarget:self withObject:nil];


// this is the main entry point for the thread
- (void) addThumbnailsToScrollView {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Top-level pool

// now place all the thumb views as subviews of the scroll view 
float xPosition = THUMB_H_PADDING;
int pageIndex = 0;
for (Page *page in self.pages) {

    // get the page bitmap image and scale it to thumbnail size
    NSString *name = [page valueForKey:@"pageBackground"];
    NSString *basePath = [[NSBundle mainBundle] pathForResource:page.pageBackground ofType:@"jpg" inDirectory:nil];
    UIImage *thumbImage = [UIImage imageWithContentsOfFile:basePath];
    thumbImage = [thumbImage imageScaledToSize:CGSizeMake(80, 100)];

    // create a ThumbImageView for each page and add it to the thumbnailScrollView
    if (thumbImage) {
        ThumbImageView *thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
        [thumbView setDelegate:self];
        [thumbView setImageName:name];
        [thumbView setImageSize:CGSizeMake(80, 100)];
        [thumbView setPageIndex:pageIndex];
        pageIndex ++;
        CGRect frame = [thumbView frame];
        frame.origin.y = 0;
        frame.origin.x = xPosition;
        [thumbView setFrame:frame];
        [thumbnailPagesScrollView addSubview:thumbView];
        [thumbView release];
        xPosition += (frame.size.width + THUMB_H_PADDING);
    }
}

[self hightlightThumbnailPageAtIndex:0];
[(UIActivityIndicatorView *)[thumbnailPagesScrollView.superview viewWithTag:100] stopAnimating];

[pool release];  // Release the objects in the pool.
}

I thought that a separate thread would exit as soon as the main login procedure was completed. Was the second call to detach a thread a new thread? Why does the application crash, but sometimes not?

thank

Jk

+3
source share
2 answers

UIKit ( UIScrollVIew) - , , NSData ( ) , , .

Apple , UIKit .

+2

thumbView thumbnailPagesScrollView , . . executeSelectorOnMainThread. , . thumbView , .

whole if , , .

. .

0

Source: https://habr.com/ru/post/1767981/


All Articles