我有一个 uitableview,其中包含每 5 秒运行一次的方法获取列表。当该记录的核心数据中有更新数据时,获取列表方法不会将最新数据更新到其数组中。因此,当我重新加载数据时,它总是显示“旧”记录。
我调用此方法,然后在 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 可以在不同列表之间切换。
最佳答案
在获取之前添加这一行:
[context setStalenessInterval: 4.0]; // allow objects to be stale for max of 4 seconds
关于objective-c - UItableview 不刷新核心数据的更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9028687/