使用Swift在CloudKit中获取记录资产 [英] Fetch Record Assets in CloudKit Using Swift

查看:83
本文介绍了使用Swift在CloudKit中获取记录资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CloudKit的新手,我无法将数据库中的资产连接到应用程序中的ImageView和TextView。在我的CloudKit数据库中,在记录类型下,我创建了一个名为公司的记录。然后,我转到公共数据->默认区域,然后创建了名为 myCompany的记录的新实例。在 myCompany中,我有两个资产,一个图像和一个.txt文件。我想将这些资产连接到我的应用程序。我不确定是否需要使用CKQuery或什么是最好的方法。任何建议将不胜感激。以下是我到目前为止的内容。请随时提供自己的反馈意见,或者有更好的方法,我很想知道。谢谢。

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
        }
    }


推荐答案

如果您知道recordId,则可以通过执行以下获取来直接获取记录:

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

或者,如果您不知道ID,则应使用以下代码查询记录,只需创建正确的NSPredicate。查询可以返回多个记录。

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)

在您的情况下,使用fetchRecordWithID选项时,代码将看起来像这样:

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
            }
        }
    }
}

这篇关于使用Swift在CloudKit中获取记录资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆