Reputation: 422
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;
}
Upvotes: 1
Views: 112
Reputation: 8546
Don't think twice. For any networking operation, use AFnetworking and for any Image web loading use SDWebImage
Upvotes: 0
Reputation: 422
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
Upvotes: 0
Reputation: 2223
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?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.
Upvotes: 3
Reputation: 23271
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
Upvotes: 0