UItableview不刷新来自核心数据的更新数据

问题描述:

我有一个可用视图,每5秒运行一次方法获取列表。当该记录的核心数据中有更新的数据时,提取列表方法不会将最新数据更新到其数组中。因此,当我重新载入数据时,它总是显示“旧”记录。UItableview不刷新来自核心数据的更新数据

我打电话给这个方法,然后在uitableview上重新载入数据。

这是我获取列表的方法:

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

}

奇怪的是表当定时器运行fetchlist不与最新更新:1。当我切换到fetchlist:2时,然后回到fetchlist:1,表格被更新为最新的。

我有一个segmentcontrol切换不同的列表。

在fetchList方法的末尾,添加以下代码的UITableView刷新数据:

[yourTableView reloadData]; 
+0

是这样做了!它不工作! – kraitz 2012-01-27 03:49:59

+0

当我在fetchlist方法中打印对象中的数据时,它读取的数组中的数据已经包含过时的数据。 – kraitz 2012-01-27 04:04:15

+2

我做到了!带上下文的refreshObject方法。 谢谢! – kraitz 2012-01-27 07:23:50

之前添加此行的获取:

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

它没有工作以及..不知道为什么。它像缓存旧数据那样可能? 我检查了数据库记录更新。但是当我运行fetchlist方法时,它保存了旧数据。 – kraitz 2012-01-27 06:25:29