CloudKit Quick Start - Using Asset and Location Fields CloudKit


Using Asset and Location Fields

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

클라우드 킷은 큰 데이터 파일을 저장하기 위한 필드형식과 위치에 의해 레코드를 페치하기 위한 필드 형식을 제공한다. 이들 데이터 형식은 클라우드킷이 이들 형식을 지원함으로서 전체적인 성능향상을 꾀하기 위한 것이다. 위치에 의해 레코드를 페치할 수 있다. 예를들어 사용자가 지정한 범위의 지도상에 레코드를 표시하기 위한 것이다.

클라우드킷에서 큰 파일 저장하기

Asset 필드 형식을 사용해 클라우드킷에 큰 데이터 파일을 저장할 수 있다. 어셋은 연관된 레코드에 점유되고 클라우드 킷은 가비지컬렉션을 수행해 준다. 클라우드킷은 또한 어셋을 효율적으로 업로드하고 다운로드한다.

코드에서 어셋 필드형식은 CKAsset 객체로 표현된다. 이 코드 조각은 리소스파일로 Artwork 레코드에 Asset 필드를 설정한다

NSURL* resourceURL = [NSURL fileURLWithPath:@"..."];
if (resourceURL) {
  CKAsset* asset = [[CKAsset alloc] initWithFileURL:resourceURL];
  artworkRecord[@"image"] = asset;
}

레코드가 저장될 때 파일이 아이클라우드에 업로드된다.
Asset필드를 저장하기 위한 비슷한 코드를 추가하고 실행한다. 레코드를 저장하려면, read Maintaining a Local Cache of CloudKit Records.

단계를 확인하기

아이클라우드에 저장한 스키마와 레코드의 변화를 확인하려면 read The General Workflow and Fetching Changes . Asset 객체로 레코드를 볼 때 클라우드킷 대시보드는 바이너리 데이터의 크기를 표시한다

../Art/ViewAsset.shot/Resources/shot_2x.png

위치 필드 추가하기

레코드가 주소나 다른 위치 데이터를 가지면 레코드에 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
}];

Location 필드인 레코드 형식을 저장하는 비슷한 코드를 앱에 작성하고 실행하자. 레코드를 저장하려면 read Maintaining a Local Cache of CloudKit Records.

단계 확인하기

클라우드킷 대시보드에 레코드를 볼 때 위치 필드의 위도와 경도를 표시한다
../Art/ViewLocation.shot/Resources/shot_2x.png

위치에 의해 레코드 페치하기

데이터베이스에 위치 데이터를 가지면, 레코드 형식, 프리디케이트, 그리고 정렬 디스크립터를 가진 쿼리를 사용하여 위치에 의한 레코드를 페치할 수 있다. 프리디케이트에 있는 Location 필드는 반드시 인덱스도어야 페치가 성공한다

이 코드 조각은 샌프란시스코에서 100000미터 안에 있는 위치를 가진 모든 레코드를 패치한다.

// Get the public database object
CKDatabase* publicDatabase = [[CKContainer defaultContainer] 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 = 10000;
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
  }
}];

이 iOS앱에서 고정된 위치의 지정된 반지름 내의 아트워크가 페치된다.
../Art/6_fetching_by_location_2x.png

위치와 맵에 대한 더 많은 사항은 read Location and Maps Programming Guide.

리캡

이 챕터에서 배운 사항은 다음과 같다
- CKAsset 필드를 레코드에 추가하여 레코드 형식에 어셋형식을 추가하고 레코드를 저장
- CLLocation 객체를 사용하여 레코드 형식에 Location 형식 추가
- 위치에 의해 객체 페치하기





덧글

댓글 입력 영역