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

时间:2012-01-27 03:40:19

标签: 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,表更新为最新的。

我有一个段控件可以在不同列表之间切换。

2 个答案:

答案 0 :(得分:1)

在获取之前添加此行:

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

答案 1 :(得分:0)

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

[yourTableView reloadData];