Good day! I make an application to view the news, the preview of the news is shown in the table.
Thus I download the data:

NSURL *url=[NSURL URLWithString:path]; NSString *dataJSON=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:Nil]; NSData *data=[dataJSON dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *rootDictionary=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; 

And I safely pull out the values ​​I need, and list them in the news preview table. Question: Is this approach correct? Or do you first need to save this data somewhere on the device and then add it to the table? At this stage, the table lags very strongly, each cell is loaded, and this slows down the scrolling. I do not know how to be.

I bring the information to the cell in the following way:

  (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; CellForNews *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; id tempObject=[[NSObject alloc]init]; tempObject=[self.arrayOfNews objectAtIndex:indexPath.row]; cell.publishDate.text=tempObject[@"date"]; NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:tempObject[@"imgPath"]]]; cell.newsImage.image=[UIImage imageWithData:data]; cell.descriptionOfThenews.text=tempObject[@"description"]; cell.titleOfTheNews.text=tempObject[@"publish_title"]; return cell; } 
  • depends only on your ideology, if you want to cache data for a long time - you can save it, for example, with CoreData, if the data is reloaded every time you enter the screen = this is a normal option - iFreeman

1 answer 1

To save or not does not affect, lags you all because you load everything synchronously (dataWithContentsOfURL) i.e. when you need to upload a new cell, you climb into the Internet for a new picture and hang up the application until you get an answer. Instead, you need some way to load asynchronously, for example, using the sendAsynchronousRequest NSURLConnection method, but there is a problem that one request may be delayed and the picture will be rendered for the previous use of the cell. The same is true by the way for json loading, but here there is an option that all this works for you in the background thread

Well, just a couple of moments on the code - for some reason you do extra actions - load json into a string, which you then overtake in NSData, although you can load directly into NSData, and also create an extra empty object in the string

 id tempObject=[[NSObject alloc]init]; 

I have certain doubts about the fact that you are not creating new cells ( [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] cells, but does not create them, usually after this line they check if nil returned this method)