保存 NSManagedObjectContext 的实例不会永久保存可变的可转换属性

发布于 2024-09-29 10:53:29 字数 1928 浏览 6 评论 0原文

我使用 Core Data + sqlite 作为数据缓存。该应用程序将文件读​​入核心数据,然后使用核心数据来运行该应用程序。保存到 NSManagedObjectContext 和文件中。不过,我注意到,如果我退出应用程序并重新加载它而不重新填充核心数据数据库,则使用 -save: 保存的一些(但不是全部)数据不会保存到数据存储中。

对我的托管对象的更改全部在主线程上一次性完成,并在所有更改完成后发送 -save: 消息。未保存的数据是可转换属性,是核心数据对象中唯一可转换属性。这是保存对象的代码:

    NSInteger columnIndex = [headers indexOfObject:questionID];
    if (NSNotFound != columnIndex) {

                    // parsedLine is a mutable array already
        NSMutableArray *parsedLine = person.dataFromFile.parsedLine;
        [parsedLine replaceObjectAtIndex:columnIndex withObject:answer];
        person.dataFromFile.parsedLine = parsedLine;

        person.questionsAnsweredByPerson = [NSNumber numberWithInt:[FileParser questionsAnsweredInRow:person.dataFromFile.parsedLine withRowHeaders:headers]];

        person.address.street.questionsAnsweredByPeopleOnStreet = [NSNumber numberWithInt:[self questionsAnsweredByPeopleOnStreet]];

        //NSLog(@"rawLineBefore:\n%@", person.dataFromFile.rawLine);
        person.dataFromFile.rawLine = [ReconstructCSV composeCSVLineFromArray:person.dataFromFile.parsedLine];
        //NSLog(@"rawLineAfter:\n%@", person.dataFromFile.rawLine);


        Voter_SurveyAppDelegate *appDelegate = (Voter_SurveyAppDelegate *)[[UIApplication sharedApplication] delegate];

        NSError *error;

        NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
        if (![managedObjectContext save:&error]) {
            // XXX inform the user there was a fatal error opening the file. Low disk space?
            NSLog(@"Unresolved error - could not save managedObjectContext - %@, %@", error, [error userInfo]);
            abort();
        }
        return YES;
    }

abort();没有被调用,所以我假设 -save;正在被正确调用。

我怀疑它是否相关,但在主线程上运行此代码后,我在不同的线程上使用新的 NSManagedObjectContext 执行 NSFetchRequest。其他线程上没有发生与核心数据相关的任何其他事情。

为什么可变形属性没有被保存?

I'm using Core Data + sqlite as a data cache. The app reads in a file into core data, then uses Core Data to run the app. Saves are done to both the NSManagedObjectContext and to the file. I've noticed, though, if I quit the app and reload it without repopulating the Core Data database, some (but not all) of the data saved using -save: is not being saved to the data store.

Changes to my managed object are all done in a single batch on the main thread, with the -save: message being sent after all changes are completed. The data that isn't being saved is a transformable attribute, the only transformable attribute in the core data object. Here's the code that saves the object:

    NSInteger columnIndex = [headers indexOfObject:questionID];
    if (NSNotFound != columnIndex) {

                    // parsedLine is a mutable array already
        NSMutableArray *parsedLine = person.dataFromFile.parsedLine;
        [parsedLine replaceObjectAtIndex:columnIndex withObject:answer];
        person.dataFromFile.parsedLine = parsedLine;

        person.questionsAnsweredByPerson = [NSNumber numberWithInt:[FileParser questionsAnsweredInRow:person.dataFromFile.parsedLine withRowHeaders:headers]];

        person.address.street.questionsAnsweredByPeopleOnStreet = [NSNumber numberWithInt:[self questionsAnsweredByPeopleOnStreet]];

        //NSLog(@"rawLineBefore:\n%@", person.dataFromFile.rawLine);
        person.dataFromFile.rawLine = [ReconstructCSV composeCSVLineFromArray:person.dataFromFile.parsedLine];
        //NSLog(@"rawLineAfter:\n%@", person.dataFromFile.rawLine);


        Voter_SurveyAppDelegate *appDelegate = (Voter_SurveyAppDelegate *)[[UIApplication sharedApplication] delegate];

        NSError *error;

        NSManagedObjectContext *managedObjectContext = [appDelegate managedObjectContext];
        if (![managedObjectContext save:&error]) {
            // XXX inform the user there was a fatal error opening the file. Low disk space?
            NSLog(@"Unresolved error - could not save managedObjectContext - %@, %@", error, [error userInfo]);
            abort();
        }
        return YES;
    }

abort(); is not getting called, so I assume -save; is getting called properly.

I doubt it is related, but after this code is run on the main thread, I perform an NSFetchRequest using a new NSManagedObjectContext on a different thread. Nothing else takes place related to Core Data on other threads.

Why isn't the transformable attribute getting saved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

雪落纷纷 2024-10-06 10:53:29

问题是可转换属性不喜欢可变对象。正如这个问题的回答所述, NSMutableDictionary 没有被保存。就我而言,它是一个 NSMutableArray。

The problem is that transformable properties don't like Mutable objects. As noted in an answer to this question, an NSMutableDictionary wasn't getting saved. In my case, it was an NSMutableArray.

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