2012-01-27 163 views
3

我有一個可用視圖,每5秒運行一次方法獲取列表。當該記錄的核心數據中有更新的數據時,提取列表方法不會將最新數據更新到其數組中。因此,當我重新載入數據時,它總是顯示「舊」記錄。UItableview不刷新來自核心數據的更新數據

我打電話給這個方法,然後在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切換不同的列表。

回答

0

在fetchList方法的末尾,添加以下代碼的UITableView刷新數據:

[yourTableView reloadData]; 
+0

是這樣做了!它不工作! – kraitz 2012-01-27 03:49:59

+0

當我在fetchlist方法中打印對象中的數據時,它讀取的數組中的數據已經包含過時的數據。 – kraitz 2012-01-27 04:04:15

+2

我做到了!帶上下文的refreshObject方法。 謝謝! – kraitz 2012-01-27 07:23:50

1

之前添加此行的獲取:

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

它沒有工作以及..不知道爲什麼。它像緩存舊數據那樣可能? 我檢查了數據庫記錄更新。但是當我運行fetchlist方法時,它保存了舊數據。 – kraitz 2012-01-27 06:25:29