kraitz
kraitz

Reputation: 345

UItableview not refreshing updated data from core data

i have a uitableview with a method fetch list that runs every 5 secs. when there is updated data in the core data for that record, the fetch list method does not update the latest into its array. thus, when i reload the data, it always shows the "old" record.

i call this method, followed by a reload data on the uitableview.

this is my fetch list method:

- (void) fetchList:(int) listNo{
// Define our table/entity to use
self.marketWatchListArray = nil;
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Market_watch" inManagedObjectContext:context]; 

// Setup the fetch request
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity]; 

Mobile_TradingAppDelegate *appDelegate = (Mobile_TradingAppDelegate *)[[UIApplication sharedApplication] delegate];

NSPredicate *getList = [NSPredicate predicateWithFormat:@"(list_id == %d) AND (userID == %@)", listNo, appDelegate.user_number];
[request setPredicate:getList];

NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"stockName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortByName]];


// Fetch the records and handle an error
NSError *error;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy]; 


if (!mutableFetchResults) {
    // Handle the error.
    // This is a serious error and should advise the user to restart the application
} 

// Save our fetched data to an array
[self setMarketWatchListArray: mutableFetchResults];
[mutableFetchResults release];
[request release];

}

strange thing is the table does not update with the latest when the timer runs fetchlist:1. When i swap to fetchlist:2, then back to fetchlist:1, the table is updated with the latest.

I have a segmentcontrol to toggle between different list.

Upvotes: 3

Views: 1155

Answers (2)

Rayfleck
Rayfleck

Reputation: 12106

Add this line before the fetch:

 [context setStalenessInterval: 4.0]; // allow objects to be stale for max of 4 seconds

Upvotes: 1

anhduongt
anhduongt

Reputation: 1119

At the end of fetchList method, you add below code to refresh data in UITableView :

[yourTableView reloadData];

Upvotes: 0

Related Questions