2011-09-22 36 views
0

我已經顯示UIAlertView與「請稍候」文本,同時加載一些數據,讓用戶知道現在正在處理的東西。NSOperationQueue Implementedation

所以我躲進了UIAlertView - (UITableViewCell的*)的tableView:(UITableView的*)的tableView的cellForRowAtIndexPath:(NSIndexPath *)indexPath功能如下

所以現在我需要知道的是我在處理NSOperationQueue以正確的方式?或者我錯過了什麼使用NSOperationQueue

這是我的代碼。請讓我知道你的想法。

感謝

-(void)buttonPressed 
{ 
alertLoading =[[UIAlertView alloc] initWithTitle:@"Loading Data" message:@"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
     av=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
     [av startAnimating]; 
     [av setFrame:CGRectMake(125, 70, 37, 37)]; 
     //[self.view addSubview:alertLoading]; 
     [alertLoading show]; 
     [alertLoading addSubview:av]; 
     [av release]; 

     NSOperationQueue *queue = [NSOperationQueue new]; 
     NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
              initWithTarget:self 
              selector:@selector(parsing) 
              object:nil]; 
     [queue addOperation:operation]; 
     [operation release]; 
     [queue release]; 
} 

-(void)parsing 
{ 
    NSString *searchUrl = [NSString stringWithFormat:@"%@profile.php?type=list&logged_id=%@&start=%d& rows=%d",ConstantURL,Reg_UserId_Trim,row_index,per_page]; 


    NSURL *xmlURL = [NSURL URLWithString:searchUrl]; 

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL]; 

    parserXML =[[XMLParser alloc]initXMLParser]; 

    profileName = [[ProfileName alloc]init]; 

    myProfileParser =[[MyProfileParser alloc]initMyProfileParser]; 

    //set the Delegate 
    [xmlParser setDelegate:parserXML]; 

    BOOL success = [xmlParser parse]; 

    if (success) 
    { 
     NSLog(@"Successfully Executed"); 
     [myTableView reloadData]; 
    } 
    else 
    { 
     NSLog(@"Error is occured"); 
    } 

    [av stopAnimating]; 

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

    [myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES]; 
} 




// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 

    } 

    [alertLoading dismissWithClickedButtonIndex:0 animated:YES]; 

    return cell; 
} 

回答

1

你可以看到this例子,這是很好的NSOpeationQueue。
還有this其中之一。

0

您不應該在創建它之後立即釋放NSOperationQueue,而是在您的類中存儲對它的引用並在您的類被釋放後釋放它。 否則有可能您的操作不會被執行。