iPhone Core Data不刷新表
我正在尝试使用核心数据编写一个应用程序,并且我已经能够成功读取和写入核心数据数据库。但是,如果我在一个视图控制器中写入数据库,则在关闭应用程序然后再次重新打开之前,其他视图控制器将看不到更改。这真是令人沮丧。我不完全确定如何使刷新 - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag
方法起作用。如何获取对我的托管对象的引用?
无论如何,这是我用来读回数据的代码。这是在 viewDidLoad 方法中。
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
//Handle the error
}
[self setNewsTitlesArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
[newsSourcesTableView reloadData];
提前感谢您的帮助!
I'm trying to write an application with Core Data, and I have been able to successfully read and write to the core data database. However, if I write to the database in one view controller, my other view controllers will not see the change until the app is closed then reopened again. This is really frustrating. I'm not entirely sure how to get the refresh - (void)refreshObject:(NSManagedObject *)object mergeChanges:(BOOL)flag
method to work. How do I get a reference to my managed object?
Anyways, here's the code I'm using to read the data back. This is in the viewDidLoad method.
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"siteName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if(mutableFetchResults == nil) {
//Handle the error
}
[self setNewsTitlesArray:mutableFetchResults];
[mutableFetchResults release];
[request release];
[newsSourcesTableView reloadData];
Thanks for help in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不完全确定您打算做什么,据我所知,您正在一个视图控制器中更改托管对象上下文,并且您希望结果在其他视图控制器中可见,这是正确的吗?无论如何,解决方案是监听 NSManagedObjectContextDidSaveNotification (保存上下文时发送)并将您希望受到更改影响的视图控制器注册为观察者:
myController 控制器中的 updateTable 选择器可能看起来像这样这个:
希望有帮助。
-奥斯卡
I am not entirely sure what do you intend to do from what I understand you are changing your managed object context in one view controller and you want the result to be visible in other view controllers is this correct?. Anyway a solution for this is to listen for the NSManagedObjectContextDidSaveNotification (which is send when the context is saved) and register as a observer the view controller you want to be afected by the changes:
The updateTable in the myController Controller the selector could look something like this:
Hope that helps.
-Oscar
实际上,我发现我将表格设置为加载为我的视图。当我将表格放入另一个视图时,一切正常。
Actually, what I found was that I had the table set to load as my view. When I put the table into another view, everything worked fine.