开发者

how to solve error when using thread?

开发者 https://www.devze.com 2023-02-02 12:27 出处:网络
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 sec

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 sharedApp开发者_StackOverflow中文版lication].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;

    }


  • 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.


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];
}


ok please explain proper Why you require thread in parsing method? in your code u use table reload method in properly in thread....

because

u cant put any thing which relavent to your VIEW in thread...

u can put only background process like parsing in it... if u want reload table after parsing u can use some flag value in your code and after parsing u load table


Try change CallParser method to

-(void)CallParser {

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

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

    [self performSelectorOnMainThread:@selector(Update_View)
                           withObject:nil 
                        waitUntilDone:NO];
    [parsing release];
    [pool release];
}

And move

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

line to Update_View method


You can't access any UI elements from a background thread. You certainly can't create views on a background thread. Use "performSelectorOnMainThread" method instead of "detachNewThreadSelector" method.

All the best.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号