1

I have a class that has a CKAsset (image file) that pulls data from CloudKit. However, I can't figure out how to initialize the CKAsset. I don't have the data at the time of initialization. The class has strings as well, but I can use "" to initialize them. What can be used to initialize a CKAsset?

Here is my class...

class Locations: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
var story: String?
var image: CKAsset

override init()
{
    self.title = "Test Title"
    self.subtitle = "Test Subtitle"
    self.coordinate = CLLocationCoordinate2D.init()
    self.story = ""
    self.image = <- How do I init the CKAsset before I have the data?
}
}
ALTVisual
  • 116
  • 10

2 Answers2

0

Morning, you will find the answer to your question buried in this code :)

func saveLeCollection(theGlob:NSURL) {

    let container = CKContainer(identifier: "iCloud.com")
    let publicDB = container.publicCloudDatabase

    let singleLink2LinkthemALL = CKRecordID(recordName: uniqReference)
    let newRecord = CKRecord(recordType: "Collection", recordID: singleLink2LinkthemALL)
    let whistleAsset = CKAsset(fileURL: theAssetURL)
    newRecord["theAsset"] = whistleAsset


    var localChanges:[CKRecord] = []
    localChanges.append(newRecord)

    let saveRecordsOperation = CKModifyRecordsOperation(recordsToSave: localChanges, recordIDsToDelete: nil)
    saveRecordsOperation.savePolicy = .ChangedKeys
    saveRecordsOperation.perRecordCompletionBlock =  { record, error in
        if error != nil {
            self.showAlert(message: error!.localizedDescription)
            print(error!.localizedDescription)
        }
        // deal with conflicts
        // set completionHandler of wrapper operation if it's the case
    }
    saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
        if error != nil {
            self.showAlert(message: error!.localizedDescription)
            print(error!.localizedDescription)
        } else {
            // deal with conflictsay
            // set completionHandler of wrapper operation if it's the case

        }
    }

    publicDB.addOperation(saveRecordsOperation)


}
user3069232
  • 8,587
  • 7
  • 46
  • 87
0

First I would say setting properties in the init() like you are doing is not going to work for a CKAsset since until you make a call and pass the record to your class you will not know the fileURL or name that CK will save a CKAsset file. However, I built this framework that you never need to have a CKAsset in the class. It handles everything. From download to cache and all you need is the recordID and the property key of your asset. You use the methods directly on the imageview. Hope this helps. https://github.com/agibson73/AGCKImage

agibson007
  • 4,173
  • 2
  • 19
  • 24