UItableview无法从Core Data中刷新更新后的数据

3

我有一个带有 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时,表格会更新到最新状态。

我有一个分段控件来在不同的列表之间切换。

2个回答

1
在 fetch 前添加这行代码:
 [context setStalenessInterval: 4.0]; // allow objects to be stale for max of 4 seconds

它没有正常工作,不确定为什么。就像它正在缓存旧数据一样,这可能吗?我已经检查了数据库,记录已更新。但是当我运行fetchlist方法时,它仍然保留旧数据。 - kraitz

0
在fetchList方法的结尾处,您可以添加以下代码来刷新UITableView中的数据:
[yourTableView reloadData];

当我在fetchlist方法中打印对象中的数据时,它所获取的数组中的数据已经包含了过期的数据。 - kraitz
2
我做到了!使用refreshObject方法刷新上下文。 - kraitz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接