objective-c - UItableview 不刷新核心数据的更新数据

标签 objective-c ios cocoa-touch core-data

我有一个 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/

相关文章:

iOS Corebluetooth 扫描速率

objective-c - 需要帮助设计 iOS/iPhone 应用程序的 API 包装器

objective-c - 我有一个用 Objective-c 编写的 iOS 应用程序和一个使用 WooCommerce 的 Wordpress 网站。我怎样才能连接它们?

ios - 获取从 UIWebView iOS 触发的请求的响应?

objective-c - CoreBluetooth——在设备锁定时发现 BLE 外围设备

ios - UIBezierPath 删除问题

objective-c - 将 View 添加到 ScrollView

iphone - 在我看来,iOS 项目未显示从 Interface Builder 构建的界面元素

iphone - 为什么我的 NSNumber 总是零?

ios - 在 UINavigationController 中设置图像时,图像未显示在模拟器中