iCloud--Using Asset and Location Attributes

来源:互联网 发布:双向视觉优化训练系统 编辑:程序博客网 时间:2024/04/30 04:33

CloudKit provides attribute types specifically for storing large data files and for fetching records by location. Use these data types to leverage the performance improvements that CloudKit provides for this type of data. You can also fetch records by location. For example, display records on a map in a user-defined region.CloudKit提供属性类型专为存储大数据文件,并通过位置获取记录。CloudKit提供这种类型的数据用来对其性能改进。您还可以通过位置获取记录。例如,在用户定义的区域地图显示记录。


1.Store Large Files in CloudKit

You can store large data files in CloudKit using the Asset attribute 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 attribute type is represented by a CKAsset object. This code fragment sets an Asset attribute in an Artwork record to a resource file.

您可以使用Asset属性类型在CloudKit存储大型数据文件。Asset由相关记录所有,CloudKit为你做垃圾回收。 CloudKit也会有效地上传和下载Assets。
在代码中,Asset属性类型是由一个CKAsset对象表示。此代码片段将设置一个Asset属性在艺术品记录的的资源文件。

// Create a URL to the local fileNSURL *resourceURL = [NSURL fileURLWithPath:@"..."];if (resourceURL){
   CKAsset *asset = [[CKAsset alloc] initWithFileURL:resourceURL];
   artworkRecord[@"image"] = asset;}

When the record is saved, the file is uploaded to iCloud.
Add similar code that saves a record type with an Asset attribute to your app and run it. To save the record, read Save Records (page 18).

Verify Your Steps 可登陆CloudKit Dashboard验证是否成功


2.Add Location Attributes
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.

如果你的记录都有一个地址或位置的数据,可以将其作为一个CLLocation对象保存在相关记录中,以后按位置提取记录。例如,你的应用程序可能会在地图上显示一个标记。
此代码段使用CLGeocoder类将字符串地址转换为一个位置对象,并把它存储在一个记录中。

CLGeocoder *geocoder = [CLGeocoder new];
   [geocoder geocodeAddressString:artwork[kArtworkAddressKey] completionHandler:^(NSArray *placemark, NSError *error){
if (!error) {
if (placemark.count > 0){
CLPlacemark *placement = placemark[0];
artworkRecord[kArtworkLocationKey] = placement.location;
}
 }else {
// insert error handling here
}
// Save the record to the database
}];

Add similar code that saves a record type with an Location attribute to your app and run it.


3.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 attribute specified in the predicate must be indexed for the fetch to work. (Attributes are indexed by default.)
This code fragment fetches all records whose locations are within 250,000 meters of San Francisco.

一旦你有位置数据在数据库中,您可以使用一个包含记录类型,谓词和排序描述符的位置查询条件来查找相关记录。在谓语中指定的位置属性必须被索引的获取工作。 (属性是默认索引)。
这段代码获取旧金山方圆25万米内的所有位置记录。

// Get the public database object
CKDatabase *publicDatabase = [[CKContainercontainerWithIdentifier:containerIdentifier] publicCloudDatabase];
// Create a predicate to retrieve records within a radius of the user's location
CLLocation *fixedLocation = [[CLLocation alloc] initWithLatitude:37.7749300longitude:-122.4194200];
CGFloat radius = 2500000; // meters
NSPredicate *predicate = [NSPredicatepredicateWithFormat:@"distanceToLocation:fromLocation:(location, %@) < %f",fixedLocation, radius];

// Create a query using the predicate
CKQuery *query = [[CKQuery alloc] initWithRecordType:@"Artwork" predicate:predicate];

// Execute the query
[publicDatabase performQuery:query inZoneWithID:nil completionHandler:^(NSArray*results, NSError *error) {
   if (error) {      // Error handling for failed fetch from public database

}else {

      // Display the fetched records   }

}]; 



4.Recap
In this chapter you learned how to:
Add an Asset type to a record type by adding a CKAsset attribute to a record and saving the record Add a Location type to a record type using a CLLocation object
Fetch objects by location



0 0