【问题标题】:UItableview not refreshing updated data from core dataUItableview 不刷新核心数据中的更新数据
【发布时间】:2012-01-27 03:40:18
【问题描述】:

我有一个带有每 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 时,表会更新为最新的。

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

【问题讨论】:

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


    【解决方案1】:

    在 fetch 之前添加这一行:

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

    【讨论】:

    • 它并没有很好地工作.. 不知道为什么。就像它缓存旧数据一样可能吗?我检查了数据库记录已更新。但是当我运行 fetchlist 方法时,它会保存旧数据。
    【解决方案2】:

    在 fetchList 方法的最后,你添加下面的代码来刷新 UITableView 中的数据:

    [yourTableView reloadData];
    

    【讨论】:

    • 当我在 fetchlist 方法中打印对象中的数据时,它获取的数组中的数据已经包含过时的数据。
    • 我做到了!使用上下文 refreshObject 方法。谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 2012-01-18
    • 2011-01-22
    • 1970-01-01
    • 2012-02-17
    • 2012-06-01
    • 1970-01-01
    • 2023-03-21
    相关资源
    最近更新 更多