简体   繁体   中英

NSOperationQueue Implemetation

I have shown a UIAlertView with "Please Wait" text while loading some data to let the user to know that something is being processing now.

So I hide the UIAlertView in the - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath Function as below

So now what I need to know is am I handling the NSOperationQueue in a correct manner? or am I missing anything in using NSOperationQueue

Here is my code. please let me know your thoughts.

Thanks

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

You can see this example which is good for NSOpeationQueue.
And also this one.

You should not release the NSOperationQueue immediately after its creation, but rather store a reference to it in your class and release it when your class is deallocated. Otherwise is possible that your operations won't be executed.

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