Relational Integrity and Validation in Core Data iCloud Syncing

和自甴很熟 提交于 2019-12-02 21:21:44

Check out this thread on Apple's Developer Forums:

https://devforums.apple.com/message/641930#641930

There is a response from an Apple employee. In a nutshell:

1) This is a known bug in Core Data iCloud syncing under current versions of iOS (5.1) and OS X (10.7.3).

2) If the relationship is non-optional with a validation predicate, syncing will cease completely. So you will need to remove the validation for the time-being to keep things flowing. However, doing so will leave the devices with mismatched data.

3) There is no official workaround for this. One approach, which is messy, would be to maintain a separate attribute which tracks the relationship. Then, you would need to scan through any objects changed via iCloud and fix the relationship if it is nil.

I am also bitten by this bug. Very frustrating to deal with customers who run into troubles. Hopefully a fix from Apple with be out soon...

In case this helps others, I have made a bit more progress working around this bug in iCloud. What I've done is disabled validation during the initial transaction log import, and enabled it again when my MOC goes to save to my main store. I still get validation errors, of course, but they happen in my MOC save method, rather than deep in the Core Data framework, so I can repair the errors (e.g. delete invalid objects).

How do I 'disable' validation? The way I am doing it is to override the KVC validation method in my NSManagedObject subclass, which I have used as the root class for all Core Data entity classes.

-(BOOL)validateValue:(__autoreleasing id *)value forKey:(NSString *)key error:(NSError *__autoreleasing *)error 
{
    if ( ![self.managedObjectContext isKindOfClass:[MCManagedObjectContext class]] ) {
        return YES;
    }
    else {
        return [super validateValue:value forKey:key error:error];
    }
}

In the override, I check the class of the managed object context, and if it is my custom class, I do the validation normally by chaining to the super method. If it is not my custom subclass, I return YES to indicate validity.

Of course, you could make this more nuanced, but you hopefully get the idea: you try to determine whether this is one of your app's standard saves or a iCloud import save, and branch accordingly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!