cloudkit error no authToken received for asset

Viewed 533

Why do I get this error when I run the following code? :

"Internal Error" (1/1000); "No authToken received for asset"

I think it has something to do with the setObject code in the last line.

let documentsDirectoryPath:NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
var imageURL: URL!

let imageData = UIImageJPEGRepresentation(self.newImage, 1.0)
let path:String = documentsDirectoryPath.appendingPathComponent(self.newImage.description)
try? UIImageJPEGRepresentation(self.newImage, 1.0)!.write(to: URL(fileURLWithPath: path), options: [.atomicWrite])
imageURL = URL(fileURLWithPath: path)
try? imageData?.write(to: imageURL, options: [.atomicWrite])

let imageAsset:CKAsset? = CKAsset(fileURL: URL(fileURLWithPath: path))


curImages = record["Images"] as! [CKAsset]
curImages.append(imageAsset!)

print("saving image")
record.setObject(curImages as CKRecordValue?, forKey: "Images")
2 Answers

This is not an answer to the specific problem (which as been solved by the accepted answer), but it solves another problem that creates the same error message, so it might be useful for somebody else:

I have an app that uses CoreData+Cloudkit with a .public database, i.e. my description for the NSPersistentCloudKitContainer uses

description.cloudKitContainerOptions!.databaseScope = .public  

Whenever I change my CloudKit schema, I have to re-initialize it using

do {
    try self.initializeCloudKitSchema()
} catch {
    print("Could not initialize schema, error \(error)")
}

This creates the error

"Internal Error" (1/1000); "No authToken received for asset"  

although I do not use any asset in my model.

I now realized that it has something to do with the .public database:
As soon as I out-comment the instruction that sets the database scope to .public, the re-initialization works without problems.
Now the CloudKit schema is independent of the database type (.private or .public). Thus, a re-initialization of the schema requires the following:

  • Set the database to .private (the default)
  • Execute the init code
  • Set the database to .public
  • Disable the init code

PS: I know I should write now a bug report, but I stopped doing so: Nearly none of my bug reports (about 15) have ever been answered or processed, so it is not worth the effort.

Related