Reload TableView Data from DB - iphone

In my app i git dat from DB by php file in internet and data shown in a Table view but i need to reload my data every minute, i can get the new data every minute but i cant replace it in the table view.
1) Do you know how to do it?
2) If you know how to get my code more simple, and delete that unNeeded codes? I'll be thanks to you.
My ViewController.m
#import "ViewController.h"
#import "CJSONDeserializer.h"
#implementation ViewController
#synthesize tblMain, rows;
NSURL *url;
NSString *jsonreturn;
NSData *jsonData;
NSError *error;
NSDictionary *dict;
UITableViewCell *cell;
static NSString *CellIdentifier;
- (void)viewDidLoad{
[super viewDidLoad];
[self GetData];
}
-(void)GetData{
url = [NSURL URLWithString:#"http://ar2.co/savola/"];
jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn);
jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
rows = [dict objectForKey:#"savola"];
NSLog(#"Array: %#",rows);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rows count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
dict = [rows objectAtIndex: indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#", [dict objectForKey:#"company"], [dict objectForKey:#"price"]];
cell.detailTextLabel.text = [dict objectForKey:#"time"];
return cell;
}
#end

To force a table to reload its information, call [self.tableView reloadData] from your view controller class. That will call all of the data source methods again, and it will update the display as well.

You should reload the table after fetching the new data from server. Like in your GetData method :
- (void)GetData
{
url = [NSURL URLWithString:#"http://ar2.co/savola/"];
jsonreturn = [[NSString alloc] initWithContentsOfURL:url];
NSLog(jsonreturn);
jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
dict = [[[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error] retain];
rows = [dict objectForKey:#"savola"];
NSLog(#"Array: %#",rows);
[yourTable reloadData];
}

You need to use reloadData method to reload your table view to display the new data.
But as you mentioned- "but i need to reload my data every minute"
So for this you can check the recieved data with existing data, and if something is changed in new data then only reload the table, else skip that.
If you reload your table every one minute unneccesary without change in data, that will be not correct and also that will slow down your app.

In this kind of situation I would recommend you look at using NSFetchedResultsController. It will automatically update you table only when it is required.

Related

Navigating in UITableView Slow with JSON Issue

I am loading 4 cities string in the table view which is working but when I select one of the cell and navigate to the other table it navigate too slow. I am using the same code below in the the other table with different link. Could you please tell me why it takes long time (~4 - 6 Sec) to get to the other view?
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL * url = [NSURL URLWithString:#"http://kalkatawi.com/jsonTest.php"];
NSData * data = [NSData dataWithContentsOfURL:url];
NSError *e = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&e];
jsonArray1 = [[NSMutableArray alloc] init];
for(int i=0;i<[jsonArray count];i++)
{
NSString * city = [[jsonArray objectAtIndex:i] objectForKey:#"city"];
[jsonArray1 addObject:city];
}
-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *tempString = [jsonArray1 objectAtIndex:indexPath.row];
cell.textLabel.text = tempString;
return cell;
}
-
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
SeconfViewController *second = [[SeconfViewController alloc] initWithNibName:#"SeconfViewController" bundle:nil];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellText = selectedCell.textLabel.text;
NSString *edit = [NSString stringWithFormat:#"http://kalkatawi.com/jsonTest.php?d=1&il=%#", cellText];
second.str2 = edit;
[self.navigationController pushViewController:second animated:YES];
}
It takes more time to navigate on the other screen because you are synchronously loading the data from the server. In iOS all the UI is done on the main thread, and by making your data call on the main thread you are blocking it. The best way I know to handle this is to use GCD (Grand Central Dispatch). It's an API in iOS that will spawn threads for you without a hassle. You just need to tell that you want to make the call to load data from the server on a background thread. When you do that, view should navigate instantaneously. You can use an Activity indicator while the data comes.
dispatch_async(dataQueue, ^{
// Load all your data here
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
});
});
Maybe because you are downloading synchronous and you are blocking the main thread maybe that is the reason that your app freeze for 4-6 sec, try to download your json asynchronous
- (void)viewDidLoad
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *response = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://kalkatawi.com/jsonTest.php"]];
NSError *parseError = nil;
jsonArray = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingAllowFragments error:&parseError];
jsonArray1 = [[NSMutableArray alloc] init]
for(int i=0;i<[jsonArray count];i++)
{
NSString * city = [[jsonArray objectAtIndex:i] objectForKey:#"city"];
[jsonArray1 addObject:city];
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
[self.myTableView reloadData];
});
});
}

How to reload a uitable when the row of a different uitable's row is selected

I'm trying to reload a UITableView when the row of a different UITableView's row is selected. One of the problems I'm having is that both the UITableView's are in the same view. I would like a selection in Table1 to change the NSMutableArray that's used to populate Table2. And then reload the table.
At the moment it works fine, but only when the app re-lauches (or the view is popped off the stack and then re-visited) and viewWillAppear is called again
Here's my code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];
[self.navigationController setNavigationBarHidden:YES animated:NO];
self.navigationController.toolbarHidden = YES;
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
selectedTableString = [prefs stringForKey:#"selectedTableString"];
if ([selectedTableString isEqualToString:#"Italy"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=it";
}
else if ([selectedTableString isEqualToString:#"Spain"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=es";
}
else if ([selectedTableString isEqualToString:#"UK"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=uk";
}
else if ([selectedTableString isEqualToString:#"Brazil"]) {
jsonStringCountry = #"http://****/v1/public/cities?country=br";
}
NSLog(#"from two t selectedTableString %#",selectedTableString);
// Download the yoodeal JSON
NSString *jsonString = [NSString stringWithContentsOfURL:[NSURL URLWithString:jsonStringCountry] encoding:NSStringEncodingConversionAllowLossy|NSUTF8StringEncoding error:nil];
NSLog(#"jsonStringCountry is %#", jsonStringCountry);
NSMutableArray *itemsTMP = [[NSMutableArray alloc] init];
// Create parser for the yoodeal api
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *results = [parser objectWithString:jsonString error:nil];
itemsTMP = [results objectForKey:#"results"];
self.displayItems = [itemsTMP copy];
}
My UITableView methods:
- (int)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MainCell"];
}
// Get item from tableData
NSDictionary *item = (NSDictionary *)[displayItems objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"name"];
[cell.textLabel setFont:[UIFont fontWithName: #"Asap-Bold" size: 14.0f]];
return cell;
}
You'll need to use the delegate method of UITablveView. The method is:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
This methods gets called when you click on the row of UITableView. Use this in following manner:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == Table1)
{
//do the changes in array you want to do.
[table2 reloadData];
}
else
{
//do the changes in array you want to do.
[table1 reloadData];
}
}
Create outlet for the table view
[self.yourTableView reloadData];
You should create a property of the UITableView called secondTableView and then use [secondTableView reloadData].
Use [tablename reloadData]; onclick.
When you are updating tableview contents, it will not refresh the view. You can only see changes once the view controller is reloaded.
For updating the table instantly you need to call the above code

self performSelector:#selector(loadData) withObject:nil - not working

I use:
self performSelector:#selector(loadData) withObject:nil...
it look like working with some command only in "loadData" but the rest are not.
Here is my viewdidload:
- (void)viewDidLoad
{
[super viewDidLoad];
[mActivity startAnimating];
[self performSelector:#selector(loadData) withObject:nil afterDelay:2];
//[mActivity stopAnimating];
}
and here is loadData:
-(void)loadData
{
[mActivity startAnimating];
NSLog(#"Start LoadData");
AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *selectData=[NSString stringWithFormat:#"select * from k_proverb ORDER BY RANDOM()"];
qlite3_stmt *statement;
if(sqlite3_prepare_v2(delegate.db,[selectData UTF8String], -1,&statement,nil)==SQLITE_OK){
NSMutableArray *Alldes_str = [[NSMutableArray alloc] init];
NSMutableArray *Alldes_strAnswer = [[NSMutableArray alloc] init];
while(sqlite3_step(statement)==SQLITE_ROW)
{
NSString *des_strChk= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3)];
if ([des_strChk isEqualToString:#"1"]){
NSString *des_str= [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
[Alldes_str addObject:des_str];
}
}
Alldes_array = Alldes_str;
Alldes_arrayAnswer = Alldes_strAnswer;
}else{
NSLog(#"ERROR '%s'",sqlite3_errmsg(delegate.db));
}
listOfItems = [[NSMutableArray alloc] init];
NSDictionary *desc = [NSDictionary dictionaryWithObject:
Alldes_array forKey:#"description"];
[listOfItems addObject:desc];
//[mActivity stopAnimating];
NSLog(#"Finish loaData");}
it give me only printing 2 line, but did not load my Data to the table, but if I copy all the code from inside "loadData" and past in "viewDidLoad", it load the data to the table.
Any advice or help please.
A few things: If you see any NSLog output at all, then the performSelector is succeeding. You should change the title of your question.
If you are trying to load data into a table, the method should end with telling the UITableView to reloadData (or a more elaborate load using begin/end updates).
If the listOfItems is the data supporting the table, get this working first by hard-coding something like this:
-(void)loadData {
listOfItems = [NSArray arrayWithObjects:#"test1", #"test2", nil];
[self.tableView reloadData];
return;
// keep all of the code you wrote here. it won't run until you remove the return
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString *string = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = string;
return cell;
// keep all of the code you probably wrote for this method here.
// as above, get this simple thing running first, then move forward
}
Good luck!

iPhone UITableView populateing from connectionDidFinishLoading

I have been trying for hours to figure this out. I have some JSON from a NSURLConnection. This is working fine and I have parsed it into an array. But I can't seem to get the array out of the connectionDidFinishLoading method. I an am getting (null) in the UITableViewCell method. I am assuming this is a scope of retain issue, but I am so new to ObjC I am not sure what to do. Any help would be greatly appreciated.
Cheers.
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
SBJSON *json = [[SBJSON alloc] init];
NSDictionary *results = [json objectWithString:responseString];
self.dataArray = [results objectForKey:#"data"];
NSMutableArray *tableArray = [[NSMutableArray alloc] initWithObjects:nil];
for (NSString *element in dataArray) {
//NSLog(#"%#", [element objectForKey:#"name"]);
NSString *tmpString = [[NSString alloc] initWithString:[element objectForKey:#"name"]];
[tableArray addObject:tmpString];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell.
//cell.textLabel.text = [self.tableView objectAtIndex:indexPath.row];
cell.textLabel.text = [self.tableArray objectAtIndex:indexPath.row];
NSLog(#"%#", tableArray);
return cell;
}
Solution:
After taking kubi and st3fan's advice with self.tableArray I found I had to reload the tableView with [self.tableView reloadData];
Get rid of the [connection release] in the 2nd line. The connection object comes in autoreleased, so this could cause crashes.
It looks like you've got a property named tableArray? If so, you're redeclaring the name in this method (you should have gotten a compiler warning).
On second thought, here's how the 2nd 1/2 of the method should look:
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithObjects:nil];
for (NSString *element in dataArray) {
[tmpArray addObject:[element objectForKey:#"name"]];
}
self.tableArray = tmpArray;
[tmpArray release];
In connectionDidFinishLoading: you use declare tableArray as a local variable. Therefore it will never be assigned to self.tableArray.
I had the same problem and I resolved it reloading the table.
I inserted this line at the end of connectionDidFinishLoading function
[self.tableView reloadData];
and it works.

UITableView isn't loading the data from my plist file what am i doing wrong?

Okay, so I have an app that I'm trying to get to populate a UITableView with the contents of a plist file. I haven't been able to find anyone else having problems doing this and how they fixed it so I'm probably just an idiot but I need to know how all the same!
I'm sure the error is somewhere in my viewDidLoad section or my tableview section...but maybe someone else can give me a better idea.
-(void)viewDidLoad {
NSString *path = [[NSBundle mainBundle] pathForResource:#"datam" ofType:#"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
self.listData = array;
[array release];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"Identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
I think [super viewDidLoad] should be foremost in the viewDidLoad method.
NSString *path = [[NSBundle mainBundle] pathForResource:#"datam" ofType:#"plist"];
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
This seems a little odd; is your file named "datam.plist"?
Try using some NSLogs() throughout your code to see what the status of your array is at any given time:
NSLog(#"%# has %i elements",ARRAY_OBJECT,[ARRAY_OBJECT count]);
Check the GDB output in XCode while the application runs to see if the results match what you expect.
self.listData = array; [array release];
Try this instead:
self.listData = [array copy]; [array release];
NSArray objects are pointers; when you set self.listData = array, you really tell anything looking for listData to look in the memory address originally occupied by array. When you release array, it vacates that memory address, thus making the pointer point to nothing.