10

I store colours in my binary Core Data store using a transformable attribute, specifying the class of the attribute as UIColor like so:

#import "CoreDataEntity+CoreDataClass.h"
#import <UIKit/UIKit.h>


NS_ASSUME_NONNULL_BEGIN

@interface CoreDataEntity (CoreDataProperties)

+ (NSFetchRequest<CoreDataEntity *> *)fetchRequest;

@property (nullable, nonatomic, retain) UIColor *transformable;
@property (nullable, nonatomic, copy)   NSString *string;

@end

NS_ASSUME_NONNULL_END

In the iOS 11 Beta this has stopped working with an error like this :

NSUnderlyingException=value for key 'NS.objects' was of unexpected class 'UIColor'. Allowed classes are '{(\n    NSDecimalNumber,\n    NSData,\n    NSUUID,\n    NSNumber,\n    NSDate,\n    NSArray,\n    NSOrderedSet,\n    NSDictionaryMapNode,\n    NSString,\n    NSSet,\n    NSDictionary,\n    NSURL,\n    NSNull\n)}'.}";
    NSUnderlyingException = "Can't read binary data from file";
}

I managed to replicate the specific problem in an XCode project on GitHub (Must be run with the XCode Beta twice to get the error).

In the demo project the store type is controlled by NSPersistentStoreDescription and setting it to NSBinaryStoreType, which I do in the AppDelegate in the exanple project, and I add objects in application didFinishLaunchingWithOptions, otherwise it's the standard template from an iOS11 app with core data. Plus a small datamodel and classes.

If you run the project twice, the first time it creates the datastore and everything is fine. The second time, the datastore tries to open and crashes the app. This problem only seems to be related to binary datastores from what I can tell, if I use an SQL backed datastore it works. However, my app is in the wild and uses binary.

I've reported it to Apple as a bug and sought help on the developer forums, but Apple has not acknowledged the bug and no help was coming.

I'm getting a bit worried as the iOS11 release date draws nearer and I have no solution, my app just won't work in iOS11.

I've tried changing the property to NSData and seeing if it was possible to just unarchive the data, but it seems it's still stored internally as a UIColor somehow and the database just won't open.

Can anyone see a workaround? I have the app in the wild, and possibly pushing out an update to convert the datastores before iOS11 could work for some, but that isn't going to guarantee all users get the fix and they could lose their data.

EDIT 1: Radar number : 33895450

EDIT 2: It just occured to me that this applies to any transformable attribute in core data, the values supported in the error message are just the default property types.

EDIT 3: Just out of curiosity I filled out all the fields for the transformable attribute (it was never required before). I added "NSKeyedUnarchiveFromData" to value transformer name of the core data entity, it should be the default, but you never know. No effect. It must be using the value transformer anyway to know that it's a UIColor. I filled in the custom class field to be UIColor, no effect.

Edit 5 : I noticed earlier that UIColor now supports NSSecureCoding, should security somehow be the issue somehow overlooked in the other store typed.

Edit : Now that iOS is released, i’ve used one of my TSIs to further escalate this. Do i get them back if i have to use one to get them to fix their software?

Edit : Apple got back to me on my TSI, they said it’s under investigation, there is no workaround, and to wait on the bug. They refunded my TSI because they couldn’t help.

Edit 8: Same problem on macOS High Sierra, with NSColor instead of UIColor.

Apple still have not given me any feedback on my actual bug report.

17
  • Can you post your radar number here? Sep 1, 2017 at 17:50
  • Sure, I added it to the end of the question Sep 1, 2017 at 18:00
  • I can reproduce this and I can't think of a workaround right now. Sep 1, 2017 at 19:26
  • Yeah. I thought that ultimately the UIColor was stored as nsdata, and core data handled the encoding as uicolor supports NSCoding. I thought that maybe setting the property to NSData would allow me to decode it, so i was suprised to see it still wouldn’t open the store, meaning somewhere along the line, it’s opened the store, understood it’s a uicolor, then stopped progress for some reason without even looking at the class. Sep 1, 2017 at 19:46
  • 1
    Thanks, think I'm going mad sometimes when i can't see a reason, but there's no way this error should exist right? Whatever is stored should leave you an option to decode it Sep 1, 2017 at 23:43

2 Answers 2

6

Well Apple got back to me, there are new persistentStore options!

The text I got from apple:

/* Allows developers to provide an additional set of classes (which must implement NSSecureCoding) that should be used while decoding a binary store. Using this option is preferable to using NSBinaryStoreInsecureDecodingCompatibilityOption. */ COREDATA_EXTERN NSString * const NSBinaryStoreSecureDecodingClasses API_AVAILABLE(macosx(10.13),ios(11.0),tvos(11.0),watchos(4.0));

/* Indicate that the binary store should be decoded insecurely. This may be necessary if a store has metadata or transformable properties containing non-standard classes. If possible, developers should use the NSBinaryStoreSecureDecodingClasses option to specify the contained classes, allowing the binary store to to be securely decoded. Applications linked before the availability date will default to using this option. */ COREDATA_EXTERN NSString * const NSBinaryStoreInsecureDecodingCompatibilityOption API_AVAILABLE(macosx(10.13),ios(11.0),tvos(11.0),watchos(4.0));

It's not immediately clear, but basically you have to supply an NSSet of classes you use as transformable attributes that comply with NSSecureCoding as an option when opening your persistent store.

An example for mine using the UIColor :

NSError *localError;
NSDictionary *options;
if (@available(iOS 11.0, *)) {
    options = @{
                NSMigratePersistentStoresAutomaticallyOption : @YES,
                NSInferMappingModelAutomaticallyOption : @YES,
                NSBinaryStoreSecureDecodingClasses : [NSSet setWithObjects:[UIColor class], nil]
               };

} else {
    // Fallback on earlier versions
    options = @{
                NSMigratePersistentStoresAutomaticallyOption : @YES,
                NSInferMappingModelAutomaticallyOption : @YES,
                };
}
NSPersistentStore *newStore = [self.psc addPersistentStoreWithType:NSBinaryStoreType configuration:@"iOS" URL:psURL options:options error:&localError];

EDIT: Adding a solution for the newer way to open core data persistent stores using NSPersistentStoreDescription. This code is based on the current core data template.

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            NSURL *defaultURL = [NSPersistentContainer defaultDirectoryURL];
            defaultURL = [defaultURL URLByAppendingPathComponent:@"CoreDataTransformableAttribBug.binary"];
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"CoreDataTransformableAttribBug"];
            NSPersistentStoreDescription *desc = [NSPersistentStoreDescription persistentStoreDescriptionWithURL:defaultURL];

            desc.type = NSBinaryStoreType;
            if (@available(iOS 11.0, *)) {
                [desc setOption:[NSSet setWithObjects:[UIColor class], nil] forKey:NSBinaryStoreSecureDecodingClasses];
            }
            _persistentContainer.persistentStoreDescriptions = @[desc];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                } else {
                    NSLog(@"Description = %@", storeDescription);
                }
            }];
        }
    }

    return _persistentContainer;
}

I also updated my gitHub project with the fix in a branch

5
  • Hi George, I still have issues. CoreData: error: -addPersistentStoreWithType:Binary configuration:(null) URL:file:///Users/klauskneupner/Library/Containers/de.kneupner.visualthinking.ibis/Data/Library/Autosave%20Information/Unsaved%20Visual%20Thinking%20with%20IBIS%20Document.ibisVT options:{ NSBinaryStoreSecureDecodingClasses = ( NSColor ); NSPersistentStoreRemoveStoreOnCleanup = 1; } ... returned error Error Domain=NSCocoaErrorDomain Code=259 "The file couldn’t be opened because it isn’t in the correct format." When do you call it? Oct 15, 2017 at 8:36
  • I found the issue. I needed to use NSSet, even for Swift. Will be posting my solution as a separate answer. Oct 15, 2017 at 8:54
  • Hi George. I now run into the next issue. After I duplicate the file I get the same issue, but only when I try to explicitly save. Autosave seems to work. Very odd. Do you have any similar issues under iOS? Jan 18, 2018 at 20:08
  • Is there maybe somewhere an encoding option? Jan 18, 2018 at 20:15
  • could you please expand on the second part of your solution? How do you actually use your persistentContainer? Where is it located and when is it called? Many Thanks in advance! Feb 5, 2018 at 5:37
3

George did all the hard work. I only applied it to Swift. Here is my solution. I put it into my NSPersistentDocument descendant.

override func configurePersistentStoreCoordinator(for url: URL, ofType fileType: String, modelConfiguration configuration: String?, storeOptions: [String : Any]? = nil) throws {
    var options = storeOptions != nil ? storeOptions! : [String:Any]()
    if #available(OSX 10.13, *) {
        options[NSBinaryStoreSecureDecodingClasses] = NSSet(object: NSColor.self)
    }
    options[NSMigratePersistentStoresAutomaticallyOption] = true
    options[NSInferMappingModelAutomaticallyOption] = true
    try super.configurePersistentStoreCoordinator(for: url, ofType: fileType, modelConfiguration: configuration, storeOptions: options)
}

Now I can read my files again. Thanks George!

4
  • Thanks! I'm not too familiar with swift, doesn't the swift Set work? The options are prefixed NS, so it wouldn't be suprising that it doesn't. But it's annoying the scant documentation doesn't even mention what type to use, I'll be raising a documentation bug too. There seems to have been a lot of changes to the core data docs, and they've removed a lot of info, and not documented new stuff. Oct 16, 2017 at 10:55
  • I am not the expert in Swift, but I would say it didn't work. Oct 16, 2017 at 18:16
  • sorry not to get back, busy with a new job rn. I got the second example just from the xcode template if you start a new project with core data included Feb 9, 2018 at 17:08
  • Thanks George. I by now found out that on macOS you don't use NSPersistentContainer, but NSPersistentDocument. My current problem is described here: stackoverflow.com/questions/48699000/… Feb 9, 2018 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.