cloudkit错误未收到资产的authToken [英] cloudkit error no authToken received for asset
问题描述
为什么在运行以下代码时出现此错误? :
Why do I get this error when I run the following code? :
内部错误(1/1000); 没有收到资产的authToken
"Internal Error" (1/1000); "No authToken received for asset"
我认为这与 setObject $ c有关$ c>代码在最后一行。
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")
推荐答案
我也遇到了这个问题。它似乎是cloudkit中的错误,而且据我所知,它是在您尝试重用资产创建链的任何部分时发生的。
I've encountered this, too. It appears to be a bug in cloudkit, and--from what I can tell--it happens when you try to re-use any part of the "asset creation chain."
换句话说,您有一些初始数据,根据该数据创建图像,将其写入文件,然后将该文件加载到 CKAsset
中,然后您将 CKAsset
加载到 CKRecrod
中。在我的实验中,如果您重复使用这些组件中的任何一个...或者如果它们恰好是相同的(即,您创建了一个图像,那么您稍后会创建一个新的但相同的图像),会看到此错误。
In other words, you have some initial data, you create an image from that data, you write it to a file, you load that file into a CKAsset
, then you load the CKAsset
into the CKRecrod
. In my experiments, if you re-use any of those components... or if they just happen to be the same (that is, you create an image, then you happen to create a new-but-identical image later) you'll see this error.
例如,以下代码在保存记录时可靠地重新创建了 no auth token错误。它所做的只是创建一个资产数组并将其放入记录中:
For example, the following code reliably recreates the "no auth token" error when saving a record. All it does is create an array of assets and places it into the record:
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
完全解决错误。
此外,即使我尝试在 不同 CKAsset中使用一个值,也会发生此错误
**已在以前的 CKAsset
中使用,例如,使用 int tempVal = 0xf
在第一个资产中,然后在另一个 CKAsset
中使用 int secondTempVal = 0xf
也会产生否
Furthermore, this error occurs even when I tried to use a value in a different CKAsset
**that was already used in a prior CKAsset
For example, using int tempVal = 0xf
in the first asset, then using int secondTempVal = 0xf
in another CKAsset
also produces the "no auth token" error.
对于我来说,我能够强制资产值始终是唯一值,从而完全解决了问题。对于您的情况,我建议以下可能的解决方法:
In my case, I was able to force the asset value to always be a unique value, and completely solved the problem. In your case, I suggest the following possible work arounds:
- 检查资产是否使用相同的图像。如果是这样,请尝试为每个新的
CKAsset
稍作修改。 - 如果必须重新使用相同的图像,请尝试保存设置每个资产后进行记录。我不知道这是否可以解决问题,并且肯定会增加您的网络流量。但是值得尝试一下,看看是否有帮助。
- 在此问题中 OP能够创建图像文件的单独副本最终解决了这个问题。
- 打开与Apple的bug。我一直不厌其烦,因为我厌倦了观看类似的错误报告多年而无人问津。但是谁知道,您可能会更幸运。
- Check if you're using identical images for your assets. If you are, try slightly modifying the images for each new
CKAsset
. - If you must re-use identical images, try saving the record after you set each asset. I do not know if that will solve the issue, and it certainly increases your network traffic. But it's worth an experiment to see if it helps.
- In this question Saving CKAsset to CKRecord in CloudKit produces error: "No authToken received for asset" the OP was able to create separate copies of the image file that ultimately solved the problem.
- Open a bug with Apple. I didn't bother doing this, as I've grown jaded watching similar bug reports sit open for years without attention. But who knows, you might have better luck.
这篇关于cloudkit错误未收到资产的authToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!