1

I have a UITableViewController class, and i download the code in viewDidLoad method. Is it okay with that code? I'm not really sure about downloading and displaying content. Because it takes a lot of time to display news, and the table scrolling is lagging. Sorry, i'm new to objective C.

@implementation NewsViewController
- (void)viewDidLoad
{


   // _sidebarButton.tintColor = [UIColor colorWithWhite:0.96f alpha:0.2f];
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    [super viewDidLoad];
    [self getJSON];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return currentCellsCount;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CellForNews *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    id tempObject=[self.arrayOfNews objectAtIndex:indexPath.row];
    cell.publishDate.text=tempObject[@"publish_date"];
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:tempObject[@"img_path"]]];
    cell.newsImage.image=[UIImage imageWithData:data];
    cell.descriptionOfThenews.text=tempObject[@"body"];
    cell.titleOfTheNews.text=tempObject[@"publish_title"];

    return cell;
}

-(void)getJSON{

    NSString *path=@"example.com";


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

    NSDictionary *newsDict=[rootDictionary objectForKey:@"publications"];
    id marker=[[NSObject alloc]init];
    self.arrayOfNews=(NSMutableArray*)[newsDict objectsForKeys:[newsDict allKeys] notFoundMarker:marker];
    currentCellsCount=[newsDict allKeys].count;


}
mikezs
  • 301
  • 1
  • 7
  • 16
  • You're using a synchronous network method on the main thread. - `stringWithContentsOfURL` – Abizern Feb 21 '14 at 07:43
  • This is the second most question on SO and there are lots of similar questions. Please search harder, looking for keywords `UITableView asynchronous "lazy loading" blocking` – CouchDeveloper Feb 21 '14 at 08:02
  • possible duplicate of [Lazy Loading of several images in UITableView](http://stackoverflow.com/questions/11583108/lazy-loading-of-several-images-in-uitableview) – CouchDeveloper Feb 21 '14 at 08:08
  • why remove my accepted answer – codercat Feb 21 '14 at 09:27

4 Answers4

3

Because it takes a lot of time to display news You should think of pre-loading your data.

and the table scrolling is lagging.

This is because you are doing a network operation synchronously, which blocks your applications Main Thread which is responsible for rendering your UI elements. So you should send your request asynchronously.

Well, once you understand the basics. Better resort to a library which can do more than that, like AFNetworking.

I have a UITableViewController class, and i download the code in viewDidLoad method. Is it okay with that code?

I can't say it is not okay, neverthless it works! But might not be ideal, and that why it is lagging?

So, may I have the freedom not answering you to the point, instead give an advice on designing the logic for app a little more better? If so read on..

What is viewDidLoad?
  • viewDidLoad is a part of your view controller life cycle, where it tells your view controller, "hey dude! all your views are here loaded and ready. Do your shit on 'em now". Yes! it is the place where you should do some action on your views. Not on your data (unless it has something to do directly to the view).

Here is where we programmers are enlightened with the age old techniques of Seperation Of Concerns and one form of it practices by iOS monks all around the globe is Model-View-Controller. So you should seek that path or another like MVVM.

Once you have these seperated, news which is data, will become completly seperated from your view, you can then think about techniques to load it faster. Like load it while initializing the app, or use the UI based lazy loading pattern of Pull to refresh or anything else.

Community
  • 1
  • 1
egghese
  • 2,088
  • 15
  • 26
0

you use background thread like any one of NSOperation queue,GCD.

Lazy Loading is very famous example that is very helpful to you what you exacted

sample App

https://developer.apple.com/library/ios/samplecode/LazyTableImages/Listings/Classes_RootViewController_m.html

codercat
  • 21,439
  • 9
  • 56
  • 84
0

this great guide is all i need. Thanks for your answers! It really helped to solve my problem. http://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

mikezs
  • 301
  • 1
  • 7
  • 16
0

Don't think twice. For any networking operation, use AFnetworking and for any Image web loading use SDWebImage

Nicolas Manzini
  • 7,809
  • 6
  • 58
  • 77