cloudkit 错误,没有收到资产的 authToken

cloudkit error no authToken received for asset

为什么我在 运行 以下代码时会出现此错误? :

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

我觉得跟最后一行的setObject代码有关系。

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")

我也遇到过这种情况。它似乎是 cloudkit 中的错误,而且——据我所知——当您尝试重新使用 "asset creation chain."

的任何部分时,它就会发生

换句话说,你有一些初始数据,你从这些数据创建一个图像,你将它写入一个文件,你将该文件加载到 CKAsset,然后你加载 CKAsset 进入 CKRecrod。在我的实验中,如果你重复使用这些组件中的任何一个......或者如果它们恰好相同(也就是说,你创建了一个图像,然后你碰巧创建了一个新的但相同的图像)你'你会看到这个错误。

例如,以下代码在保存记录时可靠地重现了 "no auth token" 错误。它所做的只是创建一个资产数组并将其放入记录中:

for (int i = 0; i <= maxPlayers; i++)
{
    int tempVal = 0xf;
    NSData *tempData = [[NSData alloc] initWithBytes:&tempVal length:sizeof(tempVal)];
    NSString *tempDataFilepath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"temp%d.dat",i]];
    [tempData writeToFile:tempDataFilepath atomically:YES];
    NSURL *tempDataURL = [NSURL fileURLWithPath:tempDataFilepath];
    someArray[i] = [[CKAsset alloc] initWithFileURL:tempDataURL ];
}

someRecord[SOME_FIELD_NAME] = someArray; 

只需将第三行更改为:

int tempVal = i; //force the temp value to be different every time

彻底解决错误。

此外,即使我尝试在 different [=12= 中使用一个值,也会发生此错误 ] **已经在之前的 CKAsset 中使用过例如,在第一个资产中使用 int tempVal = 0xf,然后在另一个 CKAsset 中使用 int secondTempVal = 0xf 也会产生"no auth token" 错误。

在我的案例中,我能够强制资产值始终是唯一值,并彻底解决了问题。对于您的情况,我建议采用以下可能的解决方法:

  1. 检查您是否为资产使用了相同的图像。如果是,请尝试为每个新 CKAsset.
  2. 稍微修改图像
  3. 如果您必须重复使用相同的图像,请尝试在设置每个资产后保存记录。我不知道这是否能解决问题,而且它肯定会增加您的网络流量。但值得一试,看看它是否有帮助。
  4. 在这个问题中 Saving CKAsset to CKRecord in CloudKit produces error: "No authToken received for asset" OP 能够创建最终解决问题的图像文件的单独副本。
  5. 打开 Apple 的 bug。我没有费心去做这件事,因为我已经厌倦了看着类似的错误报告多年无人关注地打开。但谁知道,你的运气可能会更好。

这不是特定问题的答案(已被接受的答案解决),但它解决了另一个产生相同错误消息的问题,因此它可能对其他人有用:

我有一个使用 CoreData+Cloudkit 和 .public 数据库的应用程序,即我对 NSPersistentCloudKitContainer 使用

的描述
description.cloudKitContainerOptions!.databaseScope = .public  

每当我更改 CloudKit 架构时,我都必须 re-initialize 使用

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

这会产生错误

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

虽然我没有在我的模型中使用任何资产。

我现在意识到它与.public数据库有关:
一旦我 out-comment 将数据库范围设置为 .public 的指令,re-initialization 就可以正常工作。
现在 CloudKit 模式独立于数据库类型(.private.public)。因此,架构的 re-initialization 需要以下内容:

  • 将数据库设置为.private(默认)
  • 执行初始化代码
  • 将数据库设置为 .public
  • 禁用初始化代码

PS:我知道我现在应该写错误报告,但我停止这样做了:近 none 我的错误报告(大约 15 份)已经得到答复或处理,所以它不值得付出努力。