【问题标题】:Save a CLLocationCooridnate2D array in Firestore在 Firestore 中保存 CLLocationCooridnate2D 数组
【发布时间】:2019-03-05 13:10:54
【问题描述】:

我正在尝试保存一个 CLLocationCoordinate2Darray 进入 Firestore,但出现错误:

'FIRInvalidArgumentException',原因:'不支持的类型:NSConcreteValue'

而且我真的不知道我该怎么做。

在代码的顶部我有这个:

var locations: [CLLocationCoordinate2D] = []

然后点击“保存”按钮。我有这个代码:

//Document Reference properties
        var _: DocumentReference = self.db
            //Collection into Subcollection & Documents
            .collection("rastad").document(authentication!)
            .collection("promenad").addDocument(data:
                //Insert first values
                ["Dog": txtfield_dog.text!,
                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "Locations": locations
                ], completion: { (err) in
                    if err != nil
                    {
                        print("Error adding info-details")
                        return
                    }
                    else
                    {
                        print("Succeded!")
    }

那么,我该如何解决这个问题呢?如何将 CLLocationCoordinate2D 数组插入 Firestore?而且,我以后怎样才能找回它?我真的很感激任何帮助!谢谢!

编辑:也许这个错误对你来说很清楚,但是很好。我不清楚,这就是我问这个问题的原因。如果我知道答案,我不会回答这个问题。而不是不喜欢并离开线程,您至少可以发表评论并告诉我不喜欢的原因。所以,我想我的问题会成立。

【问题讨论】:

  • 错误很明显:Firestore 不支持NSValue,这是CLLocationCoordinate2D 的序列化类型。分别保存longitudelatitude 或将坐标映射到CLLocationDegrees(又名Double)的数组。
  • 而我究竟该怎么做..?对于所有不喜欢的人,至少写下原因。

标签: ios swift google-cloud-firestore cllocation cllocationcoordinate2d


【解决方案1】:

为了分解它,Firebase 不知道 CLLocationCoordinate2D 对象是什么。但它确实知道数组是什么,它确实知道字符串、整数和双精度是什么。所以你必须将你的位置数组解析成它可以理解的东西。您可以将值存储在 Firestore 中,因为坐标实际上只是一对双精度值。您可以通过多种方式执行此操作,但最简单的可能是拆分位置数组,一种用于纬度值,一种用于经度值。例如,您可以使用let latitudes = locations.map({ $0.latitude }) 和相同的经度。然后在 Firestore 中,您可以将其更新为:

                 "Person": txtfield_person.text!,
                 "What": txtfield_what.text!,
                 "Date": txtfield_Datum.text!,
                 "Time": txtfield_Time.text!,
                 "Timer": txtfield_timer.text!,
                 "Kilometers": txtfield_km.text!,
                 "LocationLatitudes": latitudes,
                 "LocationLongitudes": longitude,
                ]

应用反向逻辑进行检索。您将需要遍历 lat 和 long 数组并组合成一个 CLLocationCoordinate2D 对象数组。

【讨论】:

  • 天啊。你刚刚解决了我的问题!你真的是个英雄,谢谢你!!
  • 另一个问题:如何将纬度和经度添加到用户默认值中? (它们必须在 viewDidLoad() 函数中)现在它们被放置在一个按钮函数中。
  • 用户默认也是一样的。它们只存储原始类型。因此,您需要将位置转换为原始值。不过,您必须阅读一些用户默认设置,它应该足够简单。祝你好运!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-12
  • 2020-09-28
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多