简体   繁体   中英

How to call a method on background thread

in my app i am unzipping a zip file(in splash view controller) later i am displaying an image and firing a timer with 4 secs to change image of the previous image view. and later in view did appear i am firing another timer with 10 secs to call next view controller(home view controller).. and there i have a button to go Story view controller...in this i am using the unzipped data...its taking some time to unzip in this case. so the images r displaying later a few seconds...it fine here

the problem is... i called the unzipping on background thread so its displaying the images quickly and its moving to home view controller..while going to next view controller by clicking a button its crashing on device but its working fine in simulator..

can any body help me out please

Thanks.

code...

-(void)viewWillAppear:(BOOL)animated{
    [self performSelectorInBackground:@selector(unXip) withObject:nil]; 
    CGRect imageFrame = CGRectMake(0,20,320,460);
    splashView.frame = imageFrame;
    splashView.image = [UIImage imageNamed:@"Default.png"];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImg) userInfo:nil repeats:NO];
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(changeImg2) userInfo:nil repeats:NO];


}

-(void)unXip{
    self.fileManager = [NSFileManager defaultManager];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    self.documentsDir = [paths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/temp", self.documentsDir];
    NSString *filePathbmlg = [NSString stringWithFormat:@"%@/temp/bmlg", self.documentsDir];

    NSString *updateURL = [[NSBundle mainBundle] pathForResource:@"bmlg" ofType:@"zip" inDirectory:@"res"];

    [fileManager createDirectoryAtPath:filePath 
           withIntermediateDirectories:NO
                            attributes:nil
                                 error:nil];


    if([fileManager fileExistsAtPath:filePath]) {
        NSLog(@"File exists at path: %@", updateURL);
    } else {
        NSLog(@"File does not exists at path: %@", updateURL);
    }

    NSLog(@"Checking update at : %@", updateURL);
    NSLog(@"Checking filepath at : %@", filePath);

    if(![fileManager fileExistsAtPath:filePathbmlg]) {  

        ZipArchive *zipArchive = [[ZipArchive alloc] init];

        if([zipArchive UnzipOpenFile:updateURL]) {

            if ([zipArchive UnzipFileTo:filePath overWrite:YES]) {
                //unzipped successfully
                NSLog(@"Archive unzip Success");
                //[self.fileManager removeItemAtPath:filePath error:NULL];
            } else {
                NSLog(@"Failure To Unzip Archive");             
            }

        } else  {
            NSLog(@"Failure To Open Archive");
        }

        [zipArchive release];
    }
}   

- (void)changeImg{

    splashView.image = [UIImage imageNamed:@"screen2.png"];

}

- (void)changeImg2{

    splashView.image = [UIImage imageNamed:@"screen3.png"];
}

- (void)viewDidAppear:(BOOL)animated{   
    [NSTimer scheduledTimerWithTimeInterval:15 target:self selector:@selector(pushCVC) userInfo:nil repeats:NO];
}

- (void)pushCVC{
    homeViewController = [[HomeViewController  alloc] initWithNibName:@"HomeView" bundle:nil];
    [self presentModalViewController:homeViewController animated:YES];
}

It's kind of hard to answer this witout any sample code. One thing to be aware of when using background threads is to make sure that all code that updates the UI runs on the main thread.

Ok, I think this is probably a threading issue. Your unXip method is modifying something on a background thread and then you try to access that "something" from another thread (main thread if you access it from UI code). You need to implement some kind of locking/synchronization mechanism (eg NSLock or NSConditionLock).

It looks a bit strange that you are using a timer to call pushCVC. Now, I don't know exactly what your app does but wouldn't it be better to call pushCVC when the unzip has finished by adding this to the end of your unXip method:

[self performSelectorOnMainThread:@selector(pushCVC) withObject:nil waitUntilDone:NO];

and removing the timer.

thanks for your comments and answers....

The problem is the unzipping process is taking a long time...so if i click start button on the home screen before the completion of the unzipping the file, its crashing...because i have to use the data from unzipped file...

Thanks again..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM