当 ManagedObjectContext 更改时,NSFetchedResultsController 不会更新

发布于 2024-12-13 18:53:28 字数 2241 浏览 1 评论 0原文

我编写了一个程序,有时我会将一些锚点移动到另一个锚点

,当我移动这些锚点时,我会重新计算两个锚点附近(锚点之前和之后)的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

a√萤火虫的光℡ 2024-12-20 18:53:28

NSFetchedResultsController fetchedObjects 属性状态:

结果数组仅包含指定实体的实例
获取请求 (fetchRequest) 及其谓词匹配。 (如果
fetch 请求没有谓词,那么结果数组包含所有
获取请求指定的实体的实例。)

结果数组反映了托管对象在内存中的状态
控制器的托管对象上下文,而不是它们的状态
持久存储。 但是,返回的数组不会更新为
插入、修改或删除托管对象。

可用性 适用于 iOS 3.0 及更高版本。

我无法说出适当的解决方法是什么。我的第一个想法是在委托实现中的 controllerDidChangeContent: 中调用 performFetch:

fetchedObjects 数组似乎只需用空实现覆盖 controllerDidChangeContent: 即可更新。这是使用 iPad 和 iOS 5.1 的 iPad 模拟器的情况。

文档和我观察到的内容之间显然存在一些差异。我没有解释。对不起。为了安全起见,我只能建议您在 controllerDidChangeContent: 中执行提取。

The NSFetchedResultsController documentation for fetchedObjects property states:

The results array only includes instances of the entity specified by
the fetch request (fetchRequest) and that match its predicate. (If the
fetch request has no predicate, then the results array includes all
instances of the entity specified by the fetch request.)

The results array reflects the in-memory state of managed objects in
the controller’s managed object context, not their state in the
persistent store. The returned array does not, however, update as
managed objects are inserted, modified, or deleted.

Availability Available in iOS 3.0 and later.

I can't say what the appropriate workaround is. My first thought is to call performFetch: in controllerDidChangeContent: in the delegate implementation.

The fetchedObjects array appears to update simply by overriding controllerDidChangeContent: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文