我有一个带有 fetch list 方法的 UITableView,该方法每 5 秒运行一次。当核心数据中有该记录的更新数据时,fetch list 方法不会将最新数据更新到其数组中。因此,当我重新加载数据时,它总是显示“旧”的记录。
我调用这个方法后,跟着在 UITableView 上重新加载数据。
这是我的 fetch list 方法:
- (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时,表格会更新到最新状态。
我有一个分段控件来在不同的列表之间切换。