从 Firebase 填充的 CLLocationCoordinate2D 数组不会保留其条目。斯威夫特 4
Posted
技术标签:
【中文标题】从 Firebase 填充的 CLLocationCoordinate2D 数组不会保留其条目。斯威夫特 4【英文标题】:CLLocationCoordinate2D array populated from Firebase doesn't keep it's entries. Swift 4 【发布时间】:2018-12-05 13:56:14 【问题描述】:我对存储CLLocationCoordinates2D from retrieving posts from Firebase. Oddly the array gets populated every time I enter
NewMapViewController. I thought that it would stay populated and only get new entries as the child observer send a new post from Firebase, but instead it's retrieving the posts every time
NewMapViewControllerloads.
My problem is, that I get to
NewMapViewControllereighter from a menu or from a notification action button.
In the second case I use
alertNotificationCoordinatesArrayin the
checkAlerts2`函数的@987654322`有问题,所以我需要它已经被填充但它是空的。
这里是代码中感兴趣的部分:
override func viewDidLoad()
super.viewDidLoad()
mapView.delegate = self
locationManager.delegate = self
// locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.allowsBackgroundLocationUpdates = true //for getting user location in background mode as well
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow //map following user
// let globalLocationManager: GlobalLocationManager
configureLocationServices()
addDoubleTap() // enabling duble tap gesture recognizer
// mapView.isUserInteractionEnabled = true
let location = locationManager.location?.coordinate
if location == nil
return
let region = MKCoordinateRegionMakeWithDistance(location!, 1000, 1000) // set mapView based on user location coordinates
mapView.setRegion(region, animated: true)
centerMapOnLocation()
// alerts coordinates to post to Firebase
// let alertDrawLatitude = alertDrawCoordinates?.latitude // not used ?
// let alertDrawLomgitude = alertDrawCoordinates?.longitude // not used ?
// let title: String? = alertNotificationType // not used ?
// var subtitle: String? = alertNotificationType // not used ?
// user alert notification. takes coordinates from alertNotificationArray( populated with firebase returning coordinate for all alerts
displayAlerts()
print("alertNotificationCoordinatesArray at loading NewMapViewController is\(alertNotificationCoordinatesArray)")
// if coming from alert notification
if NewMapViewController.checkCounter > 0
checkAlerts2()
else return
使用 Firebase 帖子的函数:
func displayAlerts()
ref = Database.database().reference()
databaseHandle = ref?.child("Community").child("Alert Notifications").observe(.childAdded, with: (snapshot) in
// defer self.dummyFunctionToFoolFirebaseObservers()
guard let data = snapshot.value as? [String:String] else return
guard let firebaseKey = snapshot.key as? String else return
// let date = data!["Date"]
// let time = data!["Time"]
let dataLatitude = data["Latitude"]!
let dataLongitude = data["Longitude"]!
let type = data["Description"]!
let id = Int(data["Id"]!)
let doubledLatitude = Double(dataLatitude)
let doubledLongitude = Double(dataLongitude)
let recombinedCoordinate = CLLocationCoordinate2D(latitude: doubledLatitude!, longitude: doubledLongitude!)
print("Firebase alerts posts retrieved")
// print("Longitude Actual DataKey is \(String(describing: firebaseKey))")
// print("fir long \((snapshot.value!, snapshot.key))")
let userAlertAnnotation = UserAlert(type: type, coordinate: recombinedCoordinate, firebaseKey: firebaseKey, title: type,id: id!)
self.userAlertNotificationArray.append(userAlertAnnotation) // array of notifications coming from Firebase
print("userAlertNotificationArray after retrieving from Firebase is : \(self.userAlertNotificationArray)")
self.alertNotificationCoordinatesArray.append(recombinedCoordinate) // array for checkig alerts on route
print("alertNotificationCoordinatesArray after retrieving from Firebase is : \(self.alertNotificationCoordinatesArray)")
self.mapView.addAnnotation(userAlertAnnotation)
)
以及来自控制台的打印:
alertNotificationCoordinatesArray at loading NewMapViewController is[]
Firebase alerts posts retrieved
userAlertNotificationArray after retrieving from Firebase is : [<fix_it_mapView.UserAlert: 0x1599f2e60>]
alertNotificationCoordinatesArray after retrieving from Firebase is : [__C.CLLocationCoordinate2D(latitude: 44.50139585197814, longitude: 11.335974854073397)]
CLLocationCoordinate2D(latitude: 44.50139585197814, longitude: 11.335974854073397)
如何修复这个被清空的数组?
【问题讨论】:
因为alertNotificationCoordinatesArray
每当你返回这个视图时都会被清空,这让我觉得这个视图正在重新定义一个新的属性。 alertNotificationCoordinatesArray
在哪里定义?它是你班级的财产吗?你在这个类中有viewDidAppear(_:)
函数吗?
@guard 工作其他恐慌。 alertNotificationCoordinatesArray
在 NewMapViewController
class 中定义,它没有 viewDidApear()
function 。我应该更改什么以使其保持这些值?
你能显示定义alertNotificationCoordinatesArray
的行吗?如果它是NewMapViewController
的属性,则在视图被关闭时它将丢失。另外,我会检查在应用程序的生命周期中调用了多少次viewDidLoad()
。每次视图加载时,它都可以通过调用displayAlerts()
来设置多个观察者。
@guard 工作其他恐慌。我懂了。作为NewMapViewController it gets emptied or just instantiated multiples times?
viewDidLoad()gets called Avery time I get to
NewMapViewController. How would I check it? should I call
displayAlertsfrom a
viewVillApear()`的属性?
对,如果您希望 alertNotificationCoordinatesArray
在 NewMapViewController
被解除并再次呈现后仍然存在,则需要将其保留在其他位置,例如 model class。
【参考方案1】:
感谢@guard 工作 else panic 指出每次我离开 VC 时我的数组都会被清空,因此解决方案只需将其放在单独的 swift 文件的结构中。
import Foundation
import CoreLocation
struct MapArray
static var alertNotificationCoordinatesArray: [CLLocationCoordinate2D] = [] // alert notification array coordinates only used to draw alerts on map and find route obstacles
【讨论】:
以上是关于从 Firebase 填充的 CLLocationCoordinate2D 数组不会保留其条目。斯威夫特 4的主要内容,如果未能解决你的问题,请参考以下文章
从 Firebase 填充数组传递 UITableViewCell 数据
从 firebase 读取数据,并在 listview 中填充
Objective-C - Firebase 从数据库中检索数据并填充到表中