当 ManagedObjectContext 更改时,NSFetchedResultsController 不会更新
我编写了一个程序,有时我会将一些锚点移动到另一个锚点
,当我移动这些锚点时,我会重新计算两个锚点附近(锚点之前和之后)的 biz 距离。计算是在后台完成的,
我使用这个标准代码来更新
+(void)commit {
// get the moc for this thread
[Tools breakIfLock];
NSManagedObjectContext *moc = [self managedObjectContext];
NSThread *thread = [NSThread currentThread];
DLog(@"threadKey commit%@" , [[self class]threadKey]);
if ([thread isMainThread] == NO) {
// only observe notifications other than the main thread
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:moc];
}
NSError *error;
if (![moc save:&error]) {
CLog(@"Error in Saving %@", error);
DLog(@"What the hell error is it");
}
else{
}
if ([thread isMainThread] == NO) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:moc];
}
//[GrabClass StopNetworkActivityIndicatorVisible];
}
+(void)contextDidSave:(NSNotification*)saveNotification {
dispatch_async(dispatch_get_main_queue(), ^{
BadgerNewAppDelegate *delegate = [BNUtilitiesQuick appDelegate];
DLog (@"currentThreadinContextDidSave: %@",[self threadKey]);
NSManagedObjectContext *moc = delegate.managedObjectContext; //delegate for main object
CLog(@"saveNotification : %@",saveNotification);
[moc mergeChangesFromContextDidSaveNotification:saveNotification];
});
//[moc performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:saveNotification waitUntilDone:YES];
}
我断点的东西,并看到距离确实得到了更新。一切都很好
但是 NSFetchedResultsController fetchedObjects 似乎没有更新并且仍然使用旧值。
怎么可能呢?
也
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
即使 NSManagedObjectContext 发生更改,
永远不会被调用。好吧,实际上我不确定 managementObjectContext
是否已更改。我怎么知道?我的意思是,将在 ManagedObjectContext 中进行更改,以确保 fetchController.fetchedObjects 中的更改。
据我所知没有缓存。我怎么也能确定这一点?
I make a program where I sometimes moves some anchor to another
When I move those anchors I would recompute distance of bizs nearby the 2 anchors (before and after anchors). The computation is done in background
I used this standard code to update stuff
+(void)commit {
// get the moc for this thread
[Tools breakIfLock];
NSManagedObjectContext *moc = [self managedObjectContext];
NSThread *thread = [NSThread currentThread];
DLog(@"threadKey commit%@" , [[self class]threadKey]);
if ([thread isMainThread] == NO) {
// only observe notifications other than the main thread
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:NSManagedObjectContextDidSaveNotification object:moc];
}
NSError *error;
if (![moc save:&error]) {
CLog(@"Error in Saving %@", error);
DLog(@"What the hell error is it");
}
else{
}
if ([thread isMainThread] == NO) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:moc];
}
//[GrabClass StopNetworkActivityIndicatorVisible];
}
+(void)contextDidSave:(NSNotification*)saveNotification {
dispatch_async(dispatch_get_main_queue(), ^{
BadgerNewAppDelegate *delegate = [BNUtilitiesQuick appDelegate];
DLog (@"currentThreadinContextDidSave: %@",[self threadKey]);
NSManagedObjectContext *moc = delegate.managedObjectContext; //delegate for main object
CLog(@"saveNotification : %@",saveNotification);
[moc mergeChangesFromContextDidSaveNotification:saveNotification];
});
//[moc performSelectorOnMainThread:@selector(mergeChangesFromContextDidSaveNotification:) withObject:saveNotification waitUntilDone:YES];
}
I break point and see that distances did get updated. Everything is fine
However the NSFetchedResultsController
fetchedObjects doesn't seem to get updated and still use the old value.
How can that be?
Also the
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
DLog(@"controllerWillChangeContent: %@", controller);
[self.tableViewA beginUpdates];
}
is never called even though the NSManagedObjectContext
has changes.
Well actually I wasn't sure if the managedObjectContext
has changed or not. How do I know? I mean will change in managedObjectContext ensure changes in fetchController.fetchedObjects
.
There is no caching as far as I know. How can I be sure of that too?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSFetchedResultsController
fetchedObjects
属性状态:我无法说出适当的解决方法是什么。我的第一个想法是在委托实现中的
controllerDidChangeContent:
中调用performFetch:
。fetchedObjects
数组似乎只需用空实现覆盖controllerDidChangeContent:
即可更新。这是使用 iPad 和 iOS 5.1 的 iPad 模拟器的情况。文档和我观察到的内容之间显然存在一些差异。我没有解释。对不起。为了安全起见,我只能建议您在
controllerDidChangeContent:
中执行提取。The NSFetchedResultsController documentation for
fetchedObjects
property states:I can't say what the appropriate workaround is. My first thought is to call
performFetch:
incontrollerDidChangeContent:
in the delegate implementation.The
fetchedObjects
array appears to update simply by overridingcontrollerDidChangeContent:
with an empty implementation. This is the case using both the iPad and the iPad simulator for iOS 5.1.There's clearly some discrepancy between the documentation and what I have observed. I have no explanation. Sorry. I can only suggest that you perform the fetch in
controllerDidChangeContent:
just to be safe.