我将数据存储在 Amazon 上的 dynamoDB 表中。我可以查询该表。在 dynamoDbObjectMapper.query 语句中,我可以打印结果。结果我想创建 GMSMakers(谷歌地图上的图钉)。但是当我调用它时,应用程序崩溃,并显示一条消息“必须从主线程调用 API 方法”。我只是似乎没有得到我需要做的事情。
所以我的问题是,是否有人可以提供一个示例来帮助我了解正在发生的事情并指导我找到解决方案。将查询“向上”的结果传递给主线程,以便我可以调用 Google SDK 函数。
我会非常感激......
这是我的代码(请温柔....)。结果:GMSMarker 调用生成此运行时异常:'GMSThreadException',原因:'API 方法必须从主线程调用'
import UIKit
import GoogleMaps
import AWSCore
import AWSMobileClient
import AWSDynamoDB
class DiscoverViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
@IBOutlet weak var mapView: GMSMapView!
var currentPositionMarker: GMSMarker!
var locationManager = CLLocationManager()
var output: AWSDynamoDBPaginatedOutput!
var paginatedOutput: AWSDynamoDBPaginatedOutput?
override func viewDidLoad() {
super.viewDidLoad()
//Hide the mapview until the currentlocation is found. unhide will be done from the event handler.
mapView.isHidden = true
mapView.delegate = self
self.locationManager.requestAlwaysAuthorization()
// For use in foreground
self.locationManager.requestWhenInUseAuthorization()
// Request current location: once.
if CLLocationManager.locationServicesEnabled() {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.requestLocation() //One time request
}
//first create query expression
let queryExpression = AWSDynamoDBQueryExpression()
queryExpression.keyConditionExpression = "#attribute1 = :value1"
queryExpression.expressionAttributeNames = [ "#attribute1": "storyUserId" ]
queryExpression.expressionAttributeValues = [ ":value1": "roger"]
let dynamoDbObjectMapper = AWSDynamoDBObjectMapper.default()
dynamoDbObjectMapper.query(Stories.self, expression: queryExpression) { (output: AWSDynamoDBPaginatedOutput?, error: Error?) in
if error != nil {
print("The request failed. Error: \(String(describing: error))")
}
if output != nil {
print(output!.items)
for stories in output!.items {
let story = stories as? Stories
print("Pin read \(story!._storyUserId!)")
print("Pin read \(story!._storyName!)")
print("Pin read \(story!._storyDateTime!)")
print("Pin read \(story!._storyAudioFilename!)")
print("Pin read \(story!._storyLongitude!)")
print("Pin read \(story!._storyLatitude!)")
print("Creating pin")
let positionMarker = GMSMarker()
positionMarker.position.latitude = story!._storyLatitude as! Double
positionMarker.position.longitude = story!._storyLongitude as! Double
positionMarker.title = story!._storyName
positionMarker.snippet = story!._storyAudioFilename
positionMarker.iconView = currentPositionMarkerView
positionMarker.map = self.mapView
self.showPins(result: output!)
} else {
print("No data read from the table")
}
}
// Do any additional setup after loading the view, typically from a nib.
}