core-data icon indicating copy to clipboard operation
core-data copied to clipboard

Running first time ...moody Failed to create file at ModelVersionType.swift

Open Sun3 opened this issue 9 years ago • 2 comments

Running first time I receive crash ...moody Failed to create file. This happens in the ModelVersionType.swift in the extension NSManagedObjectContext {...} It crashes on line let psc = NSPersistentStoreCoordinator(...)

Any ideas? I have made all of the changes for the CloudKit setup. Thank you.

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///Users/name/Library/Developer/CoreSimulator/Devices/50A111d9-B60C-49D0-d037-CAD21D8G96CC/data/Containers/Data/Application/3DE20E67-C970-4A56-A138-538D8F0420A6/Documents/YnBsaXN0MDDUAQIDBAUGCQpYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hvdmGyVCR0b9ASAAGGoKIHCFUkbnVbbC8QFBQ3L/RDZAr0ISZbweTAix7l2EpRXxAPTlNLZXllZEFyY2vpdmCy0RsMEHJvb3SAAQgRGiMtMjc6TFdpbHEAAAAAAAABAQAAAAAAAAANAAAAAAAAAAAAAAAAAAAAcw==.moody options:(null) ... returned error Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved." UserInfo={reason=Failed to create file; code = 2} with userInfo dictionary {
    reason = "Failed to create file; code = 2";
}
fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=512 "The file couldn’t be saved." UserInfo={reason=Failed to create file; code = 2}: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.1.101.15/src/swift/stdlib/public/core/ErrorType.swift, line 50
extension NSManagedObjectContext {
    public convenience init<Version: ModelVersionType>(concurrencyType: NSManagedObjectContextConcurrencyType, modelVersion: Version, storeURL: NSURL, progress: NSProgress? = nil) {
        if let storeVersion = Version(storeURL: storeURL) where storeVersion != modelVersion {
            migrateStoreFromURL(storeURL, toURL: storeURL, targetVersion: modelVersion, deleteSource: true, progress: progress)
        }

        let psc = NSPersistentStoreCoordinator(managedObjectModel: modelVersion.managedObjectModel())   // CRASH HAPPENS HERE...

        try! psc.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: storeURL, options: nil)
        self.init(concurrencyType: concurrencyType)
        persistentStoreCoordinator = psc
    }
}

Sun3 avatar Jan 16 '16 17:01 Sun3

Hi Sun3 I have come across this error when setting up my project as well. The issue it the ubiquityToken method in the MoodyStack.swift file. This method returns a string which has a forward slash in the string. As a quick workaround I changed the following line from

return NSKeyedArchiver.archivedDataWithRootObject(token).base64EncodedStringWithOptions(NSDataBase64EncodingOptions())

to

return NSKeyedArchiver.archivedDataWithRootObject(token).base64EncodedStringWithOptions(NSDataBase64EncodingOptions()).stringByReplacingOccurrencesOfString("/", withString:"")

I don't know if this will interfere with the migration and versioning code, as I have only started to look over the code, so you should test this if you need the code for an app

johnodowd avatar Jan 25 '16 14:01 johnodowd

@johnodowd That worked great. I appreciate the help, thank you.

Sun3 avatar Jan 25 '16 20:01 Sun3