简体   繁体   English

iCloud和核心数据。 NSFetchedResultsController在初始同步时不会更新

[英]iCloud and Core Data. NSFetchedResultsController doesn't update on initial sync

iCloud and Core Data are working great. iCloud和Core Data运行良好。 Except... 除了...

On initial app launch (existing iCloud data is there), NSFetchedResultsControllers don't update as the received data comes through. 在初始应用程序启动时(现有的iCloud数据存在),NSFetchedResultsControllers不会在接收到的数据通过时更新。 NSFetchedResultsController delegates just don't get called. NSFetchedResultsController委托只是不被调用。 On force quitting and relaunching the app all the data is there as it should be. 在强制退出并重新启动应用程序时,所有数据都应该存在。

Core data code is the same as the excellent tutorial here . 核心数据代码与此处的优秀教程相同。 I have a feeling this code isn't wrong as I've used this for another app, and didn't have this problem. 我觉得这个代码没有错,因为我已经将它用于另一个应用程序,并且没有这个问题。

Other info: My managed object context is initialised with main queue concurrency. 其他信息:我的托管对象上下文初始化为主队列并发。 The only thing I've managed to figure out is that I can catch the initial data as it comes through - the below function gets called a few seconds after the app is initially launched. 我唯一能想到的是,我可以捕获初始数据 - 在应用程序最初启动后几秒钟就会调用以下函数。 However, although the data comes through, existing fetched results controllers don't seem to be updated accordingly (but creating them again shows the data). 但是,尽管数据通过,但现有的获取结果控制器似乎没有相应更新(但是再次创建它们会显示数据)。


- (void)storesWillChange:(NSNotification *)notification {
    NSManagedObjectContext *context = self.managedObjectContext;

    [context performBlockAndWait:^{
        NSError *error;

        if ([context hasChanges]) {
            BOOL success = [context save:&error];

            if (!success && error) {
                // perform error handling
                NSLog(@"%@",[error localizedDescription]);
            }
        }

        [context reset];
    }];

}


So: What can I do to start pinpointing why NSFetchedResultsControllers aren't updated as they should be? 那么:我该怎么做才能开始查明为什么NSFetchedResultsControllers没有按原样更新?

I am guessing a bit here, but think because you are resetting the context immediately after saving, that the NSFetchedResultsController delegate methods aren't picking up on this because of the reset . 我在这里猜一点,但想想因为你在保存后立即重置上下文,NSFetchedResultsController委托方法由于重置而没有在这上面。 What I mean is the delegate methods are intended to deal with things like a selection of records changing within the current result set (inserts, updates or deletes of the records currently tracked by the fetched results controller). 我的意思是委托方法旨在处理当前结果集中更改的记录选择(插入,更新或删除当前由提取的结果控制器跟踪的记录)。

When you perform a context reset you blow all of this away, and hence these delegate methods don't get called as what they were tracking doesn't exist any longer in a form that it recognises. 当您执行上下文重置时,您将所有这些都吹走,因此这些委托方法不会被调用,因为它们跟踪的内容不再以它识别的形式存在。

Whenever I reset a context due to incoming iCloud updates I usually ensure that I recreate my fetched results controller for the newly reset context . 每当我由于传入的iCloud更新而重置上下文时,我通常会确保为新重置的上下文重新创建我的获取结果控制器。

Force quitting the app will force the fetched results controller to be recreated on relaunch, hence showing the data as you expect to see it. 强制退出应用程序将强制在重新启动时重新创建获取的结果控制器,从而显示您希望看到的数据。

Initialise your managed object context when your application launch. 在应用程序启动时初始化托管对象上下文。 and then sync with iCloud and reload table it will work fine. 然后与iCloud同步并重新加载表它将正常工作。 I also did same thing when i am getting same problem what you have. 当我遇到同样的问题时,我也做了同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM