swift Swift - CloudKit - 使用资产和位置字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift Swift - CloudKit - 使用资产和位置字段相关的知识,希望对你有一定的参考价值。

See: https://developer.apple.com/library/content/documentation/DataManagement/Conceptual/CloudKitQuickStart/AddingAssetsandLocations/AddingAssetsandLocations.html

01. Store Large Files in CloudKit

You can store large data files in CloudKit using the Asset field type. Assets are owned by the associated 
record, and CloudKit handles garbage collection for you. CloudKit also efficiently uploads and downloads 
assets.

In code, the Asset field type is represented by a CKAsset object. This code fragment sets an Asset field 
in an Artwork record to a resource file.

   // Create a URL to the local file
var resourceURL = URL(fileURLWithPath: "…")
if resourceURL {
    var asset = CKAsset(fileURL: resourceURL)
    artworkRecord["image"] = asset
}

When the record is saved, the file is uploaded to iCloud.

02. Add Location Fields

If your record has an address or other location data, you can save it as a CLLocation object in the record 
and later fetch records by location. For example, your app might display pins representing the records on 
a map.

This code fragment uses the CLGeocoder class to convert a string address to a location object and stores 
it in a record.

var geocoder = CLGeocoder()
geocoder.geocodeAddressString(artwork[kArtworkAddressKey], completionHandler: {(_ placemark: [Any], _ error: Error?) -> Void in
    if error == nil {
        if placemark.count > 0 {
            var placement = placemark[0] as? CLPlacemark
            artworkRecord[kArtworkLocationKey] = placement.location
        }
    }
    else {
        // insert error handling here
    }
    // Save the record to the database
})

03. Fetch records by location

Once you have location data in your database, you can fetch records by location using a query 
containing a record type, a predicate, and a sort descriptor. The Location field specified in 
the predicate must be indexed for the fetch to succeed.

This code fragment fetches all records whose locations are within 100,000 meters of San Francisco.

// Get the public database object

var publicDatabase: CKDatabase? = CKContainer.default().publicCloudDatabase
    // Create a predicate to retrieve records within a radius of the user's location
var fixedLocation = CLLocation(latitude: 37.7749300 as? CLLocationDegrees ?? CLLocationDegrees(), longitude: -122.4194200 as? CLLocationDegrees ?? CLLocationDegrees())
var radius: CGFloat = 100000
    // meters
var predicate = NSPredicate(format: "distanceToLocation:fromLocation:(location, %@) < %f", fixedLocation, radius)
    // Create a query using the predicate
var query = CKQuery(recordType: "Artwork", predicate: predicate)
// Execute the query
publicDatabase?.perform(query, inZoneWith: nil, completionHandler: {(_ results: [Any], _ error: Error?) -> Void in
    if error != nil {
        // Error handling for failed fetch from public database
    }
    else {
        // Display the fetched records
    }
})

For Location Services and Maps also see: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/Introduction/Introduction.html#//apple_ref/doc/uid/TP40009497



以上是关于swift Swift - CloudKit - 使用资产和位置字段的主要内容,如果未能解决你的问题,请参考以下文章

swift Swift - CloudKit - 获取用户ID

swift Swift - CloudKit - 推送通知

swift Swift - CloudKit共享

swift Swift - CloudKit - 维护本地缓存

swift Swift - CloudKit - 部署架构

swift Swift - CloudKit订阅 - 5