2

I want to fetch all of the location records from a user that is currently logged in.

This creates the location record on CloudKit:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last!
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    addCrumbPoint(center)
    let locationRecord = CKRecord(recordType: "location")
    locationRecord["location"] = location
    let publicData = CKContainer.defaultContainer().publicCloudDatabase
    publicData.saveRecord(locationRecord) { record, error in
    }
}

But how would I go about pulling out the location records for only the currently logged in user?

I want to use this to create a breadcrumb of previous journeys on a map but just getting it to print a list would be a great start!

Here is my code so far:

 func getLocationAsync(complete: (instance: CKRecordID?, error: NSError?) -> ()) {
    let container = CKRecordID(recordName: "Location")
    publicDB.fetchRecordWithID(location) { fetchedLocation, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(instance: nil, error: error)
        } else {
            print("fetched Location \(recordID?.recordName)")
            complete(instance: recordID, error: nil)
        }
    }
}
Tom Wicks
  • 785
  • 2
  • 11
  • 28

1 Answers1

1

If your goal is to save the breadcrumbs locations of an user and not sharing it to other users, then use the private database. Like that you can retrieve all the location records of the user.

If you want to use the public DB, then add an entry of type CKReference to the Location record pointing to the user record. So that you can use a predicate based on the user recordID

Nicolas Rosa
  • 121
  • 2
  • Thanks, I want all the location data to go to CloudKit but I only want to show a single users data on this view. Can you show me how to implement your bottom solution? – Tom Wicks Apr 07 '16 at 11:32
  • Tom, your approach is sadly flawed; if the user crosses his path, the save will fail and if two users cross each other's location the save will fail too. You need to focus too on ensuring you send the minimum amount of data to the cloud in the largest chunks possible to too in order to keep things as efficient as possible. The idea also relies on the user having a wifi connection as he is moving around as well, although entirely possible, is a high risk. Better save your breadcrumb in core data, group it altogether in a blob of data and save it as a single record. – user3069232 Apr 07 '16 at 13:51
  • Implementation is easy, but I don't recommend using the publicDB to store user's location like that. It will be rejected by Apple. In private DB and only accessible by the user itself is ok. – Nicolas Rosa Apr 12 '16 at 07:07