CloudKit giving "Asset File Not Found" error

548 Views Asked by At

I'm using this code to upload an asset, given a path:

func uploadFile(filePath: String) {
    let fileURL = NSURL(fileURLWithPath: filePath)
    let asset = CKAsset(fileURL: fileURL)

    let assetRecord = CKRecord(recordType: CloudManager.mediaRecordTypeName())
    assetRecord.setObject(asset, forKey: CloudManager.mediaAssetKey())

    println("asset before: \(asset)")
    println("asset file url before: \(asset.fileURL)")

    CKContainer.defaultContainer().publicCloudDatabase.saveRecord(assetRecord) {
            (record: CKRecord?, error: NSError?) -> Void in
            if record == nil || error != nil {
                println("error: \(error)")
                return
            }

        let asset = record!.objectForKey(CloudManager.mediaAssetKey()) as! CKAsset?

    }
}

I know my path points to an existing file - when it accesses the file using UIImage(contentsOfFile: filePath), it works. The path looks like this:

/var/mobile/Containers/Data/Application/2491A1A6-55C7-4236-B0DA-FEA894799DF2/Documents/MediaCache/85CDCF7C-9DFF-40E8-A8F4-B60F0157DA98.jpg

So I use this same file path in this function, and here's my log:

asset before: <CKAsset: 0x1701830c0; path="/var/mobile/Containers/Data/Application/2491A1A6-55C7-4236-B0DA-FEA894799DF2/Documents/MediaCache/85CDCF7C-9DFF-40E8-A8F4-B60F0157DA98.jpg", UUID=0DC9CE7B-25B7-4D3D-967E-6544ABE5D552>

asset file url before: file:///var/mobile/Containers/Data/Application/2491A1A6-55C7-4236-B0DA-FEA894799DF2/Documents/MediaCache/85CDCF7C-9DFF-40E8-A8F4-B60F0157DA98.jpg

error: Optional(<CKError 0x170050740: "Asset File Not Found" (16/3002); "Can't read the file">)

If i modify the passed in filePath, to point to a file which really doesn't exist, it gives me the same result. It therefore looks like it is failing to find the file. Since I know the file exists, perhaps it's the format I'm passing in my filePath.

If I comment out the assetRecord.setObject(asset line, it succeeds without any errors, of course with the record!.objectForKey being nil.

0

There are 0 best solutions below