Fetch Record Assets in CloudKit Using Swift

2019-06-04 23:20发布

问题:

I am new to CloudKit and I am having trouble connecting the assets in my database to the ImageView and TextView in my app. In my CloudKit database, under record types, I created a record named "Companies". I then went to Public Data->Default Zone and then created a new instance of the record called "myCompany". In "myCompany" I have two assets, an image and a .txt file. I want to connect those assets to my app. I am not sure if I need to use CKQuery or what is the best approach. Any advice would be much appreciated. Below is what I have so far. Feel free to give feedback of what I have or if there's a better way, I would love to know. Thanks.

import UIKit
import CloudKit

class LearnViewController: UIViewController {

@IBOutlet weak var theImage: UIImageView!
@IBOutlet weak var theText: UITextView!


let myRecord = CKRecord(recordType: "Companies" )

var image: UIImage!

let database =  CKContainer.defaultContainer().publicCloudDatabase


func loadCoverPhoto(completion:(photo: UIImage!) -> ()) {
    dispatch_async(
        dispatch_get_global_queue(
            DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)){
                var image: UIImage!
                let coverPhoto = self.myRecord.objectForKey("Picture") as CKAsset!
                if let asset = coverPhoto {

                    if let url = asset.fileURL {
                        let imageData = NSData(contentsOfFile: url.path!)!

                        image = UIImage(data: imageData) 
                    }
                }

                completion(photo: image)
    }
}


 override func viewDidLoad() {
    super.viewDidLoad()

    loadCoverPhoto() { photo in
        dispatch_async(dispatch_get_main_queue()) {
            self.theImage.image = photo
        }
    }

回答1:

You could get the record directly if you know the recordId by performing a fetch like:

database.fetchRecordWithID(CKRecordID(recordName: recordId), completionHandler: {record, error in

Or if you don't know the ID you should query for the record with something like the code below. just create the right NSPredicate. A query can return more than 1 record.

var query = CKQuery(recordType: recordType, predicate: NSPredicate(value: true))
var operation = CKQueryOperation(query: query)
operation.recordFetchedBlock = { record in
    // is this your record...
}
operation.queryCompletionBlock = { cursor, error in
    self.handleCallback(error, errorHandler: {errorHandler(error: error)}, completionHandler: {
        // ready fetching records
        })
}
operation.resultsLimit = CKQueryOperationMaximumResults;
database.addOperation(operation)

In your case when using the fetchRecordWithID option the code would look like this:

override func viewDidLoad() {
    super.viewDidLoad()

    database.fetchRecordWithID(CKRecordID(recordName: recordId), completionHandler: {record, error in
        self.myRecord = record
        loadCoverPhoto() { photo in
            dispatch_async(dispatch_get_main_queue()) {
                self.theImage.image = photo
            }
        }
    }
}