iCloud--Using Asset and Location Attributes

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 file
		NSURL *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 = [[CKContainer
containerWithIdentifier:containerIdentifier] publicCloudDatabase];
// Create a predicate to retrieve records within a radius of the user's location
CLLocation *fixedLocation = [[CLLocation alloc] initWithLatitude:37.7749300
longitude:-122.4194200];
CGFloat radius = 2500000; // meters
NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"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



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章