Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to solve error when using thread?

I have following error msg in console when using NSThread "Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now..."

I have submit my sample code here

- (void)viewDidLoad {

    appDeleg = (NewAshley_MedisonAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[self tblView1] setRowHeight:80.0];
    [super viewDidLoad];
    self.title = @"Under Ground";


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [NSThread detachNewThreadSelector:@selector(CallParser) toTarget:self withObject:nil];

}

-(void)CallParser {


    Parsing *parsing = [[Parsing alloc] init];
    [parsing DownloadAndParseUnderground];

    [parsing release];
    [self Update_View];
    //[myIndicator stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;




}

here "DownloadAndParseUnderground" is the method of downloding data from the rss feed and

-(void) Update_View{


    [self.tblView1 reloadData];
}

when Update_View method is called the tableView reload Data and in the cellForRowAtIndexPath create error and not display custom cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    static NSString *CellIdentifier = @"Cell";

    CustomTableviewCell *cell = (CustomTableviewCell *) [tblView1 dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"customCell"
                                      owner:self 
                                    options:nil];

        cell = objCustCell; 
        objCustCell = nil;

    }
like image 482
ChandreshKanetiya Avatar asked Jan 06 '11 04:01

ChandreshKanetiya


People also ask

What is a thread error?

The "SYSTEM THREAD EXCEPTION NOT HANDLED" is a common Windows 10 error that occurs mainly due to an outdated or incompatible driver. What makes it trickier to fix than other BSODs is that multiple drivers can cause the issue and sometimes your computer may not detect them all.

What are problems with threads?

When using threads, it can cause increased complexity, and debugging your code can become much more difficult. It is possible to add logic to make sure data is synchronized across threads, but too much reliance on synchronization can lead to performance issues, which affects an application's scalability.

How are the thread handled?

Thread management is handled by the thread library in user space, which is very efficient. However, if a blocking system call is made, then the entire process blocks, even if the other user threads would otherwise be able to continue.


2 Answers

  • if there is a crash, there is a backtrace. Please post it.

  • Method names start with lowercase letters, are camelCased, and do not contain underscores. Following these conventions will make your code easier to read by other iOS programmers and learning these conventions will make it easier to understand other iOS programmer's code.

You can't directly or indirectly invoke methods in the main thread from background threads. Your crash and your code both indicate that you are freely interacting with the main thread form non-main threads.

The documentation on the use of threads in iOS applications is quite extensive.

like image 97
bbum Avatar answered Sep 19 '22 17:09

bbum


Your problem should come because you load your UIViewController from a thread that's not the main thread. Tipically when you try to charge data before loading the view. To arrange this you can try to do this 1. Add a method to load your viewcontroller with just one param

-(void)pushViewController:(UIViewController*)theViewController{
[self.navigationController pushViewController:theViewController animated:YES];}

2.Change your code (commented below) in your asynchronous loading to "PerformSelectorOnMainThread"

    -(void)asyncLoadMyViewController
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    MyViewController *myVC = [[myVC alloc] initWithNibName:@"myVC" bundle:nil ];
   [self performSelectorOnMainThread:@selector(pushViewController:) withObject:myVC waitUntilDone:YES];
   //     [self.navigationController pushViewController:wnVC animated:YES];
    [wnVC release];
    [pool release];
}
like image 35
Daniel COHEN Avatar answered Sep 17 '22 17:09

Daniel COHEN