2016-08-14 9 views
2

Ich habe Probleme beim Abrufen der Standorte aus Cloudkit. Der Standort wird hochgeladen, aber wenn ich versuche, sie auszudrucken und zu laden, werden sie nicht heruntergeladen. Ich bekomme keine Fehler.Probleme beim Abrufen von Daten aus CloudKit

Diese Funktion lädt die Lage zu Wolkenjungen:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
    { 
     let location = locations.last 
     let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) 
     let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.015, longitudeDelta: 0.015)) 
     self.mapView.setRegion(region, animated: true) 
     self.locationManager.stopUpdatingLocation()// 
     let locationRecord = CKRecord(recordType: "location") 
     locationRecord.setObject(location, forKey: "location") 
     let publicData = CKContainer.defaultContainer().publicCloudDatabase 
     publicData.saveRecord(locationRecord) { record, error in 
     } 
      if error == nil 
      { 
       print("Location saved") 
      } 
     event1 = locations 
    } 

diese Funktion wird die Standorte von Wolkenjungen holt:

func loadLocation() 
    { 
     let locations = [CKRecord]() 
     let publicData1 = CKContainer.defaultContainer().publicCloudDatabase 
     let query1 = CKQuery(recordType: "location", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray:nil)) 
     publicData1.performQuery(query1, inZoneWithID: nil) { (results: [CKRecord]?, error: NSError?) -> Void in 
      if let locations = results 
      { 
       self.locations = locations 
       print(locations) 
      } 
     } 
    } 
+0

Können Sie näher erläutern, was das Problem genau ist? Wird 'CLLocationCoordinate2D' von' CKRecords' erzeugt? Zeigt es Pins auf einer Karte an? –

+0

Momentan kann ich CLLocationCoordinate2D nicht erstellen. Ich denke auch nicht, dass die Orte überhaupt abgeholt werden. @grimfrog – Steve

+0

Die Klammern sind falsch im Speichercode. Aber du musst wissen, dass das, was du hast, nicht richtig kompiliert wird? – Rob

Antwort

3

Also um dies zu tun, machte ich einen Komponententest, der übergibt:

// 
// CloudKitLocationsTests.swift 
// 

import XCTest 
import UIKit 
import CoreLocation 
import CloudKit 

class CloudKitLocationsTests: XCTestCase { 

    let locations = [ CLLocation(latitude: 34.4, longitude: -118.33), CLLocation(latitude: 32.2, longitude: -121.33) ] 

    func storeLocationToCloud(location:CLLocation) { 
     let locationRecord = CKRecord(recordType: "location") 
     locationRecord.setObject(location, forKey: "location") 
     let publicData = CKContainer.defaultContainer().publicCloudDatabase 
     publicData.saveRecord(locationRecord) { (records, error) in 
      if error != nil { 
       print("error saving locations: \(error)") 
      } else { 
       print("Locations saved: \(records)") 
      } 
     } 
    } 

    func fetchLocationsFromCloud(completion: (error:NSError?, records:[CKRecord]?) -> Void) { 
     let query = CKQuery(recordType: "Location", predicate: NSPredicate(value: true)) 
     CKContainer.defaultContainer().publicCloudDatabase.performQuery(query, inZoneWithID: nil){ 
      (records, error) in 
      if error != nil { 
       print("error fetching locations") 
       completion(error: error, records: nil) 
      } else { 
       print("found locations: \(records)") 
       completion(error: nil, records: records) 
      } 
     } 
    } 

    func testSavingLocations(){ 

     let testExpectation = expectationWithDescription("saveLocations") 
     var n = 0 
     for location in self.locations { 
      let locationRecord = CKRecord(recordType: "Location") 
      locationRecord["location"] = location 
      let publicData = CKContainer.defaultContainer().publicCloudDatabase 
      publicData.saveRecord(locationRecord) { (records, error) in 
       if error != nil { 
        print("error saving locations: \(error)") 
       } else { 
        print("Locations saved: \(records)") 
       } 
       n += 1 
       if n >= self.locations.count { 
        testExpectation.fulfill() 
       } 
      } 
     } 

     // do something then call fulfill (in callback) 

     waitForExpectationsWithTimeout(10){ error in 
      if error != nil { 
       XCTFail("timed out waiting on expectation: \(testExpectation)") 
      } 
     } 

    } 

    func testFetchingLocations(){ 
     let testExpectation = expectationWithDescription("FetchLocations") 

     fetchLocationsFromCloud(){ (error, records) in 
      if error != nil { 
       XCTFail("error fetching locations") 
      } else { 
       XCTAssertGreaterThan(records!.count, 0) 
      } 
      // do something then call fulfill (in callback) 
      testExpectation.fulfill() 
     } 

     waitForExpectationsWithTimeout(10){ error in 
      if error != nil { 
       XCTFail("timed out waiting on expectation: \(testExpectation)") 
      } 
     } 

    } 


} 

Beachten Sie, dass Sie nicht übereinstimmende Position/Standort hatten. Außerdem mache ich einen Index, um den Feldwert zu setzen.

Führen Sie das funktioniert es. Das Abrufen des Standorts aus dem Standortmanger-Rückruf hat nichts mit CloudKit zu tun, daher sollten Sie in der Lage sein, dies nach Bedarf zu verbinden.

Eine andere Sache: Ich habe die Option aktiviert, um Ihnen die Abfrage in ID-Feld für die Location-Record-Typ zu ermöglichen.

+0

Ich versuche, die fetchFromThe Cloud-Funktion aufzurufen: loadLocation ((error, self.locArray)) und erhalte den Fehler: "Wert des Typs kann nicht konvertiert werden (NSError ?, [CKRecord]?)" (alias '(Optional, Optional>)') zum erwarteten Argumenttyp '(Fehler: NSError?, Datensätze: [CKRecord]?) -> Void' "@Rob – Steve

+0

Zeig mir den Code. Klingt, als würdest du nicht die nachlaufende Schließung richtig machen. Sehen Sie sich an, wie der Test diese Methode aufruft: Er stellt die Vervollständigung nicht als Parameter bereit, sondern hat nach dem Aufruf eine Schließung. @Caleb – Rob

1

Wenn Ihr Problem ein Array von CLLocation abzurufen ist, versuchen Sie dies:

publicData1.performQuery(query1, inZoneWithID: nil) { records, error in 
    var locations = [CLLocation]() 
    if let records = records { 
     for record in records { 
      if let location = record["location"] as? CLLocation { 
       locations.append(location) 
      } 
     } 
    } 
} 
+0

Ich habe einen Ausdruck ("h") nach der Zeile "locations.append (location)" hinzugefügt und es wird nicht gedruckt, so dass ich nicht sicher bin, ob es so weit in der Funktion – Steve

+0

die "für" -Zeile ist wann Drucken funktioniert nicht mehr – Steve

Verwandte Themen