Core Data not saving transformable NSMutableDictionary

寵の児 提交于 2019-11-30 21:54:41

You've declared myDict as an NSMutableDictionary which is a big red flag.

Managed object attributes should never be a mutable type

Chances are, you're using myDict something like this:

someConfig.myDict[@"someKey"] = @"someValue";
[context save:&error];

The problem is, you're not calling a setter method of someConfig so you've done nothing to inform it that an attribute has been changed. And even though you're calling save:, the context doesn't bother saving unchanged objects.

Strictly speaking, you could probably get away with calling [someConfig didChangeValueForKey:@"myDict"] every time you change myDict. I wouldn't recommend it though because it's easy to forget and error prone.

It would better to declare myDict as non-mutable and use it like this:

@property (nonatomic, retain) NSDictionary *myDict;
...
NSMutableDictionary *updatedDict = [someConfig.myDict mutableCopy];
updatedDict[@"someKey"] = @"someValue";
someConfig.myDict = [updatedDict copy];
[context save:&error];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!